【发布时间】:2012-11-16 12:52:57
【问题描述】:
我在我的应用程序中编写了这个方法,它工作正常。我决定为它创建一个 DLL 文件。 但是同样的方法给了我错误object reference not set to an instance of an object in dll。 代码是
public void Try(string conStr, string storedProcedure)
{
try
{
//create a connection string
sqlCon = new SqlConnection(conStr);
// create command
sqlCmd = new SqlCommand(sqlCmd.CommandText, sqlCon);
//add command type
sqlCmd.CommandType = CommandType.StoredProcedure;
// add the stored procedure name
sqlCmd.CommandText = storedProcedure;
//Open connection
sqlCon.Open();
// Execute transaction
sqlCmd.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
// Close connection
sqlCon.Close();
}
错误发生在:
sqlCmd = new SqlCommand(sqlCmd.CommandText, sqlCon);
可能是什么问题??
【问题讨论】:
-
这是因为您的
sqlCmd为空,并且您正尝试使用它来创建自己的实例。在该行中,您正在创建 SqlCommand 的新实例,并且在创建其对象之前访问其CommandText。