簡體   English   中英

C#中的簡單數據庫連接錯誤

[英]Simple database Connection error in C#

我收到以下錯誤消息:

invalidOperationException was unhandled

在以下代碼中:

private void btnInsert_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=DASTGIRKHAN\\SQLEXPRESS;Initial Catalog=DBProject;Integrated    Security=True;Pooling=False");
    conn.Open();
    SqlCommand cmd = new SqlCommand("Insert INTO EmployeeRecord Values(" + tfCode.Text   + ",'" + tfName.Text + "','" + tfCell.Text + "','" + tfAdrs + "',)");
    cmd.BeginExecuteNonQuery();
    cmd.ExecuteNonQuery();
    conn.Close();
    MessageBox.Show("Inserted Successfully");
}

調用BeginExecuteNonQuery方法( msdn )並且未在連接字符串中指定“Asynchronous Processing = true”時,將引發InvalidOperationException異常。

您還應該設置與命令的連接:

SqlCommand cmd = new SqlCommand("Insert INTO EmployeeRecord Values(" + tfCode.Text   + ",'" + tfName.Text + "','" + tfCell.Text + "','" + tfAdrs + "')", conn);

出現InvalidOperationException

名稱/值對“Asynchronous Processing = true”未包含在定義此SqlCommand的連接的連接字符串中。 SamingConnection在流操作期間關閉或丟棄。

抱歉,您的代碼有很多錯誤。 讓我展示一種不同的方法

private void btnInsert_Click(object sender, EventArgs e)
{
    string cnString = @"Data Source=DASTGIRKHAN\\SQLEXPRESS;
                        Initial Catalog=DBProject;
                        Integrated Security=True;";
    string cmdText = @"Insert INTO EmployeeRecord 
                       Values(@code,@fname,@cell,@adr)";
    using(SqlConnection conn = new SqlConnection(cnString))
    using(SqlCommand cmd = new SqlCommand(cmdText, conn))
    {
         conn.Open();
         cmd.Parameters.AddWithValue("@code", Convert.ToInt32(tfCode.Text));
         cmd.Parameters.AddWithValue("@fname", tfName.Text );
         cmd.Parameters.AddWithValue("@cell", tfCell.Text  );
         cmd.Parameters.AddWithValue("@adr", tfAdrs.Text);
         int rowsInserted = cmd.ExecuteNonQuery();
         if(rowInserted > 0) 
              MessageBox.Show("Inserted Successfully");
         else
              MessageBox.Show("Insert failes");
    }
}

你的錯誤的主要原因是kmatyaszek的回答,但這只是冰山一角。

您應該始終在諸如連接之類的一次性對象周圍使用using語句 這將確保在異常情況下關閉和處理連接。

您應該使用參數化查詢來創建命令以避免Sql注入和解析問題。 例如, tfName文本框中的單引號可能會導致語法錯誤。

BeginExecuteNonQuery的調用排除了對ExecuteNonQuery的調用,並且需要調用EndExecuteNonQuery

最后, ExecuteNonQuery的結果會告訴您插入是否成功。

最后一點,我從連接字符串中刪除了Pooling=False
你沒有說什么為什么要避免他非常有用的優化。

我打印那個SQL文本。 看起來對我來說是一個不平衡的撇號。

更好的是,使用為您綁定參數的.NET類。 更容易和更好的SQL注入投影。

什么是tfCode,tfName,tfCell,tfAdrs 我假設他們是文本框控件? 如果是這樣,你使用tfAdrs而不是tfAdrs.Text

還為該命令分配連接字符串並刪除其他空格
“綜合安全”

為什么復雜化自己,使用Parameterized Insert而不是連接,這很容易引起SQL注入。

SqlCommand command1 = new SqlCommand("INSERT INTO EmployeeRecord VALUES(@tfCode, @tfName, @tfCell, @tfAdrs)", conn);

command1.Parameters.AddWithValue("@tfCode", trCode);
command1.Parameters.AddWithValue("@tfName", tfName);
command1.Parameters.AddWithValue("@tfCell", tfCell);
command1.Parameters.AddWithValue("@tfAdrs", tfAdrs);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM