SqlCommand C# 问题

发布于 2024-07-24 08:26:00 字数 850 浏览 5 评论 0原文

下面的代码引发以下异常。

错误消息:

对象引用未设置为 对象的实例。 堆栈跟踪:
在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, 布尔 returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

我不知道为什么..

sqlcon = new SqlConnection(strSqlconnection);

SqlCommand sqlcomSMCheckin = new SqlCommand("prc_CheckIn", sqlcon);

sqlcomSMCheckin.CommandType = CommandType.StoredProcedure;

sqlcomSMCheckin.Parameters.Add("@Description", SqlDbType.VarChar).Value = "My App";

sqlcomSMCheckin.CommandTimeout = this.iCommandTimeOut;

if (sqlcon.State == ConnectionState.Closed)
{
   sqlcon.Open();
}

if (sqlcomSMCheckin != null)
{
    sqlcomSMCheckin.ExecuteNonQuery(); // error here
    sqlcomSMCheckin.Dispose();
}

The below piece of code that throws the following exception..

Error Message:

Object reference not set to an
instance of an object. Stack Trace:
at
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior,
Boolean returnStream) at
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

I am lost as to the reason why..

sqlcon = new SqlConnection(strSqlconnection);

SqlCommand sqlcomSMCheckin = new SqlCommand("prc_CheckIn", sqlcon);

sqlcomSMCheckin.CommandType = CommandType.StoredProcedure;

sqlcomSMCheckin.Parameters.Add("@Description", SqlDbType.VarChar).Value = "My App";

sqlcomSMCheckin.CommandTimeout = this.iCommandTimeOut;

if (sqlcon.State == ConnectionState.Closed)
{
   sqlcon.Open();
}

if (sqlcomSMCheckin != null)
{
    sqlcomSMCheckin.ExecuteNonQuery(); // error here
    sqlcomSMCheckin.Dispose();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

就此别过 2024-07-31 08:26:00

您好,

代码看起来不错(我建议您使用 using 子句,如之前的答案中向您演示的那样)。 我想知道问题是否不是来自存储过程。 尝试调试它,或者至少在存储过程的开头和结尾添加一些日志记录,以确保它每次都能正常退出。

HI

The code seems OK (I recommend that you use the using clause as demonstrated to you in a previous answrer). I wonder if the problem is notfrom within the stored procedure. Try to debug it, or at least add some log recording atthe begining and end of your stored procedure to make sure that it exits OK every time.

夜清冷一曲。 2024-07-31 08:26:00

不能 100% 确定这里发生了什么 - 但你能尝试一下这段代码吗?

using(sqlcon = new SqlConnection(strSqlconnection))
{
   using(SqlCommand sqlcomSMCheckin = new SqlCommand("dbo.prc_CheckIn", sqlcon))
   {
       sqlcomSMCheckin.CommandType = CommandType.StoredProcedure;

       sqlcomSMCheckin.Parameters.Add("@Description", SqlDbType.VarChar, 50)
            .Value = "My App";

       sqlcomSMCheckin.CommandTimeout = this.iCommandTimeOut;

       sqlcon.Open();
       sqlcomSMCheckin.ExecuteNonQuery();
       sqlcon.Close();
   }
}

我用“dbo.prc_CheckIn”替换“prc_CheckIn”,指定 VARCHAR 参数的最大长度(根据需要调整),使用 {} 块将所有内容包装起来 - 就这样。

您仍然遇到同样的错误吗?

马克

Not 100% sure what's going on here - but can you try this snippet of code?

using(sqlcon = new SqlConnection(strSqlconnection))
{
   using(SqlCommand sqlcomSMCheckin = new SqlCommand("dbo.prc_CheckIn", sqlcon))
   {
       sqlcomSMCheckin.CommandType = CommandType.StoredProcedure;

       sqlcomSMCheckin.Parameters.Add("@Description", SqlDbType.VarChar, 50)
            .Value = "My App";

       sqlcomSMCheckin.CommandTimeout = this.iCommandTimeOut;

       sqlcon.Open();
       sqlcomSMCheckin.ExecuteNonQuery();
       sqlcon.Close();
   }
}

I replace "prc_CheckIn" with "dbo.prc_CheckIn", I specify a max length on the VARCHAR parameter (adjust as needed), wrapped everything in using {} blocks - that's about it.

Do you still get the same error??

Marc

绅士风度i 2024-07-31 08:26:00

sqlCon 是类级别变量吗?多次调用同一方法是否有问题? 类中有静态的东西吗? 您使用多线程吗?

Is sqlCon a class level variable, do you have issues with the same method being called multiple times? Is there anything static in the class? Are you using multiple threads?

混吃等死 2024-07-31 08:26:00

确保存储过程“prc_CheckIn”存在于您要连接的 SQL Server 中。 也许它拼写错误,或者连接中的用户无法访问。

Make sure the sproc "prc_CheckIn" exist in the SQL Server you are connecting to. Maybe it's misspelled and or not accessible to the user in the connection.

月下客 2024-07-31 08:26:00

您应该始终在执行 sql 命令后立即关闭 sqlconnection

如果这不是函数的一部分,则不需要使用 new 恢复它 - 只需打开它就足够了。

You should always close your sqlconnection immediately after executing the sqlcommand

If this is not part of a function, then reinstating it with new is not necessary - just opening it will be sufficient.

旧时光的容颜 2024-07-31 08:26:00

您是否尝试过禁用连接池和/或多个活动结果集? 我忘记了连接字符串选项,但如果您调用连接字符串生成器,它应该位于高级属性网格上。 或者访问 www.connectionstrings.com。

另外,这是否是一个“用户实例”数据库,就像您使用 SQL Express 将 .mdf 文件添加到 Visual Studio 项目时一样?

Have you tried disabling connection pooling and/or multiple active result sets? I forget the connection string option for that but if you invoke the connection string builder it should be on the advanced property grid. Or on www.connectionstrings.com.

Also, is this a "user instance" database like when you add a .mdf file to a Visual Studio project using SQL Express?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文