c# 動態生成DLL文件出錯.請各們幫看看以下代碼!

mslyjin 2011-05-17 10:52:49
#region 生成公式DLL文件在(FormulasFile)里
/// <summary>
/// 生成公式DLL文件在(FormulasFile)里
/// </summary>
/// <param name="ClassName">類名</param>
public string Creadll(string ClassName)
{
string ft = "";
try
{
CSharpCodeProvider CSPC=new CSharpCodeProvider();

//

ICodeCompiler complier = CSPC.CreateCompiler();
;
//
CompilerParameters CPS = new CompilerParameters();
CPS.GenerateExecutable = false;
CPS.GenerateInMemory = false;
CPS.OutputAssembly = "ANA.dll";
CPS.IncludeDebugInformation = false;

CPS.ReferencedAssemblies.Add("System.dll");
CPS.ReferencedAssemblies.Add("System.Data.dll");
StringBuilder valu = Creaforu(sql_table, formu_table, NAME, accment);
Connectiondata(sql_table, accment);
StringBuilder DLLSB = new StringBuilder();
DLLSB.Append("using System;\r\n");
DLLSB.Append("using System.Data;\r\n");
DLLSB.Append("using System.Data.SqlClient;\r\n");
DLLSB.Append("namespace GenClass\r\n");
DLLSB.Append("{\r\n");
string sqlst="Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\TEST\\dgsweda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
DLLSB.Append(" public class " + ClassName + "\r\n");
DLLSB.Append(" {\r\n");
DLLSB.Append(" private static string sqlconstring=@\"" + sqlst + "\";\r\n");
DLLSB.Append(" private SqlConnection getcon()\r\n");
DLLSB.Append(" {\r\n");
DLLSB.Append(" SqlConnection con = new SqlConnection(sqlconstring);\r\n");
DLLSB.Append(" return con;\r\n");
DLLSB.Append(" }\r\n");
DLLSB.Append(" public DataTable Datatable(string sqlstirng)\r\n");
DLLSB.Append(" {\r\n");
DLLSB.Append(" SqlConnection con = getcon();\r\n");
DLLSB.Append(" SqlDataAdapter ada = new SqlDataAdapter(sqlstirng, con);\r\n");
DLLSB.Append(" DataTable da = new DataTable();\r\n");
DLLSB.Append(" ada.Fill(da);\r\n");
DLLSB.Append(" return da;\r\n");
DLLSB.Append(" }\r\n");
DLLSB.Append(" private string HID;\r\n");
DLLSB.Append(" public " + ClassName + "(string _HID)\r\n");
DLLSB.Append(" {\r\n");
DLLSB.Append(" HID = _HID;\r\n");
DLLSB.Append(" }\r\n");
DLLSB.Append(STB.ToString() + "\r\n");
DLLSB.Append(valu.ToString() + "\r\n");
DLLSB.Append(" }\r\n");
DLLSB.Append("}\r\n");
CompilerResults RESU = complier.CompileAssemblyFromSource(CPS, DLLSB.ToString());
 DLLSB = null;
}
catch (Exception me)
{ MessageBox.Show(me.Message); }
return ft;
}

#endregion
...全文
207 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
振浩 2011-08-06
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 x8bits 的回复:]
建议一,增加引用System.Xml.dll,因为System.Data.dll需要它:
C# code
CPS.ReferencedAssemblies.Add("System.Xml.dll");


建议二,把using放入namespace里面去
C# code

DLLSB.Append("namespace GenClass\r\n");
DLLSB.Append(……
[/Quote]

我也出现了这个情况啊,可是两种方法都不行。
x8bits 2011-05-17
  • 打赏
  • 举报
回复
是不是说调用方法是
mslyjin 2011-05-17
  • 打赏
  • 举报
回复
在线等待,大虾帮忙
mslyjin 2011-05-17
  • 打赏
  • 举报
回复
有没有方法可以解决?

就是添加Sql DataTable 或查询数据库时出错!!

其它方法,变量都没有问题!
mslyjin 2011-05-17
  • 打赏
  • 举报
回复


添加,DataTable 變量時無法編譯出DLL文件!!
deknight 2011-05-17
  • 打赏
  • 举报
回复
估计是组合时出了问题
x8bits 2011-05-17
  • 打赏
  • 举报
回复
出啥错呢?
x8bits 2011-05-17
  • 打赏
  • 举报
回复
建议一,增加引用System.Xml.dll,因为System.Data.dll需要它:
CPS.ReferencedAssemblies.Add("System.Xml.dll");


建议二,把using放入namespace里面去

DLLSB.Append("namespace GenClass\r\n");
DLLSB.Append("{\r\n");
DLLSB.Append("using System;\r\n");
DLLSB.Append("using System.Data;\r\n");
DLLSB.Append("using System.Data.SqlClient;\r\n");


试试吧,祝你好运!
mslyjin 2011-05-17
  • 打赏
  • 举报
回复
CPS.OutputAssembly = "ANA.dll";声明了生成的DLL文件

错误信息就是,你无法生成"ANA.DLL"文件!!,在你指定的文件夹内无法找到此文件!!


系统提示,无法找到"ANA.dll"所依赖的文件!!
begintransaction 2011-05-17
  • 打赏
  • 举报
回复
把错误信息代出来!
x8bits 2011-05-17
  • 打赏
  • 举报
回复
把错误信息贴出来看看吧。
lovebibi168 2011-05-17
  • 打赏
  • 举报
回复
你 有删代码么?
mslyjin 2011-05-17
  • 打赏
  • 举报
回复
编译是有问题!!



编译在项目里?
他山玉 2011-05-17
  • 打赏
  • 举报
回复
我是想不通你为什么要动态生成一个dll。你把这个类直接编译在你的项目中不行吗?
x8bits 2011-05-17
  • 打赏
  • 举报
回复
到底是编译有问题还是调用时有问题啊?能不能讲清楚
mslyjin 2011-05-17
  • 打赏
  • 举报
回复
那怎么样才能引用System.Data.dll呢?
mslyjin 2011-05-17
  • 打赏
  • 举报
回复
SqlDataAdapter 函数不使用open();
他山玉 2011-05-17
  • 打赏
  • 举报
回复
我想起来一个问题。默认新创建的项目好像是不包含 System.Data(在 System.Data.dll 中)引用的。
你只生成一个源码的文件,没有添加项目引用,可能会导致无法编译。
mslyjin 2011-05-17
  • 打赏
  • 举报
回复
class xxxxx
{
private static string sqlconstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\TEST\dgsweda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
private SqlConnection getcon()
{
SqlConnection con = new SqlConnection(sqlconstring);
return con;
}
public DataTable Datatable(string sqlstirng)
{
SqlConnection con = getcon();
SqlDataAdapter ada = new SqlDataAdapter(sqlstirng, con);
DataTable da = new DataTable();
ada.Fill(da);
return da;
}
private string HID;

public xxxxx(string _HID)
{
HID = _HID;
}
private int _应出勤()
{
int redata = 0;

DataTable da = Datatable("select * from PER where HID='" + HID + "'");
for (int i = 0; i < da.Rows.Count; i++)
{
redata = int.Parse(da.Rows[i]["PER01"].ToString().Trim());
}
return redata;
}
private int 应出勤
{
get { return _应出勤(); }
}

public int _全勤奖()
{
int retype = 0;

if (应出勤 == 22)
retype = int.Parse("20");
else
retype = int.Parse("60");

return retype;
}
private int 全勤奖
{
get { return _全勤奖(); }
}

}

这是我用编译生成的代码!!!

是可以运行的
他山玉 2011-05-17
  • 打赏
  • 举报
回复
有没有在IDE里面试过你生成的这段代码到底能不能编译通过呢?
加载更多回复(4)

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧