C# SQL查询异常

发布于 2024-09-10 14:02:18 字数 927 浏览 8 评论 0原文

我在 .NET 2.0 中使用 C#,并尝试访问和操作数据库。我可以根据需要从数据库中读取任意多次,并且一切正常,但是一旦我尝试插入一个项目,我就会收到以下错误消息:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

我已尝试查找此内容,但我能够找到修复程序要么不起作用,要么不适用。

我有以下代码:

using (SqlConnection conn = new SqlConnection(SQLConnectionString))
{
    SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT [Col1] FROM [Table1] WHERE [Col2]='" + val2 + "'", conn);
    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet);

    if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count != 1)
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
        cmd.ExecuteNonQuery();
    }
}


注意:我确信我的权限设置正确,因为 Visual Studio 可以使用相同的 SQLConnectionString 插入。另外,我对数据库还很陌生,所以如果我做任何明显错误的事情,请告诉我。

谢谢。

I'm using C# in .NET 2.0 and I'm trying to access and manipulate a database. I can read as many times from the DB as I want and everything works, but as soon as I try to insert an item I get the following error message:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

I've tried to look this up, but the fixes I was able to find either didn't work or weren't applicable.

I have the following code:

using (SqlConnection conn = new SqlConnection(SQLConnectionString))
{
    SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT [Col1] FROM [Table1] WHERE [Col2]='" + val2 + "'", conn);
    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet);

    if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count != 1)
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
        cmd.ExecuteNonQuery();
    }
}

Note: I'm sure I have permissions set up properly, since Visual Studio can insert with the same SQLConnectionString. Also, I am still fairly new to databases, so if I'm doing anything blantently wrong, please let me know.

Thanks.

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

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

发布评论

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

评论(9

a√萤火虫的光℡ 2024-09-17 14:02:19

在调用 cmd.ExecuteNonQuery() 之前调用 conn.Open()。

Call conn.Open() before calling cmd.ExecuteNonQuery().

风追烟花雨 2024-09-17 14:02:19

在执行命令之前,您必须打开连接

 conn.Open()  
    SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);  
    cmd.ExecuteNonQuery();  
    conn.Close()

You have to open connection, before executing command

 conn.Open()  
    SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);  
    cmd.ExecuteNonQuery();  
    conn.Close()
凑诗 2024-09-17 14:02:19

您需要在调用 con.ExecuteNonQuery 之前调用 con.open 并在其之后调用 con.Close 。 Dataadapter.fill 正在早期代码的幕后为您完成此操作。

You need to call con.open before you call con.ExecuteNonQuery and con.Close after it. Dataadapter.fill is doing it for you behind the scenes in the earlier code.

娇纵 2024-09-17 14:02:19

使用 cmd 之前检查 conn.IsOpen 属性。而且 SqlCommand 也是一次性对象,最好的做法是将其包含在“using”块中。

Check conn.IsOpen property before using cmd. And SqlCommand is disposable object too, its better practice to enclose it into "using" block.

吖咩 2024-09-17 14:02:19

上面没有人检查以确保连接已打开。
我在我的生产代码中使用它:

using (var conn = new SqlConnection(SQLConnectionString))
{
    conn.Open();
    if (conn.State == ConnectionState.Open)
    {
        SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT [Col1] FROM [Table1] WHERE [Col2]='" + val2 + "'", conn);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);

        if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count != 1)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
            cmd.ExecuteNonQuery();
        }
    }
}

No-one above is checking to make sure the connection opened.
I use this in my production code:

using (var conn = new SqlConnection(SQLConnectionString))
{
    conn.Open();
    if (conn.State == ConnectionState.Open)
    {
        SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT [Col1] FROM [Table1] WHERE [Col2]='" + val2 + "'", conn);
        DataSet dataSet = new DataSet();
        dataAdapter.Fill(dataSet);

        if (dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count != 1)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
            cmd.ExecuteNonQuery();
        }
    }
}
予囚 2024-09-17 14:02:18

如果连接未打开,.Fill() 将打开连接,然后在连接完成后将其关闭(仅当它自己打开时)。这就是 Fill 起作用的原因。请参阅 MSDN

.ExecuteNonQuery() 不会执行此操作,因此您需要在

conn.Open();

ExecNonQuery 之前或 Fill 之前手动打开连接。

当您使用“using 块”时,您不需要显式 Close() 连接,但这不会是错误的。

The .Fill() opens the connection if it was not open and then closes it after it's done (only if it did open it itself). That's why that Fill did work. See MSDN.

But the .ExecuteNonQuery() doesn't do that, so you need to manually open the connection, with a

conn.Open();

either just before the ExecNonQuery or before the Fill.

As you are using a "using block", you don't need to explicitly Close() the connection, but that would not be wrong.

瑾兮 2024-09-17 14:02:18

您似乎没有打开连接来执行更新(异常告诉您这一点)。

尝试一下

SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
            conn.Open();    
            cmd.ExecuteNonQuery();
            conn.Close();

您可以在 MSDN 上获取更多详细信息和工作示例,网址为 http://msdn .microsoft.com/en-us/library/sd2728ad.aspx

You don't appear to be opening the connection to perform your update (the exception tells you this).

Try this

SqlCommand cmd = new SqlCommand("INSERT INTO [Table1] ([Col1], [Col2]) VALUES ('" + val1 + "', '" + val2 + "')", conn);
            conn.Open();    
            cmd.ExecuteNonQuery();
            conn.Close();

You can get more details and working example on MSDN at http://msdn.microsoft.com/en-us/library/sd2728ad.aspx

£噩梦荏苒 2024-09-17 14:02:18

当您调用 dataAdapter.Fill(dataSet); 时,它将自动打开和关闭连接。
因此,您需要在使用 insert 语句之前重新打开连接,或者将 DataAdapter 替换为 SqlCommand 并保持连接打开,直到执行 insert 语句。

When you call dataAdapter.Fill(dataSet); it will automatically open and close connection.
So you need to reopen connection before using insert statement, or replace DataAdapter with SqlCommand and keep connection opened until you execute insert statement.

枯寂 2024-09-17 14:02:18

你没有打开连接。

本页向您展示如何使用 using 语句打开 SqlConnection。

你的朋友 C# 使用声明

You didn't open the connection.

This page shows you how to open a SqlConnection with the using statement.

Your Friend the C# Using Statement

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