开始交易时出错

时间:2018-06-27 03:12:13

标签: c# .net sql-server

我试图在SQL Server上开始事务,但是它返回一个错误,我无法确定真正的问题是什么。所以这是我尝试过的一些代码。

这是错误:

enter image description here

代码:

SqlConnection connection = new SqlConnection("Data Source=LOCALHOST\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=SSPI;User ID = xxxx; Password=xxx;");

DateTime dt = dateTimePicker1.Value.Date;
dt = dt.AddDays(60); 

string selectQuery = "BEGIN TRANSACTION UPDATE tester SET 
test_ad=@dateTimePicker1, test_ud=@dt, test_pd=@dt WHERE 
test_name=@textBox1;INSERT INTO records(testr_status, testr_name, testr_ad, 
testr_ud, testr_pd, apte_name)VALUES(@testr_status, testr_name = @comboBox1, 
testr_ad = @dateTimePicker1, testr_ud = @dt, testr_pd = @dt COMMIT";

connection.Open();

SqlCommand command = new SqlCommand(selectQuery, connection);
command.Parameters.AddWithValue("@dateTimePicker1",this.dateTimePicker1.Value.Date);
command.Parameters.AddWithValue("@textBox1", this.textBox1.Text);
command.Parameters.AddWithValue("@comboBox1",this.comboBox1.SelectedItem);
command.Parameters.AddWithValue("@testr_status",SqlDbType.VarChar);
command.Parameters.AddWithValue("@dt", dt);

int iResult = command.ExecuteNonQuery();

if (iResult > 0)
    MessageBox.Show("Successfully saved ", "Error",MessageBoxButtons.OK, MessageBoxIcon.Information);
else
    MessageBox.Show("Record not saved ", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);

command.ExecuteNonQuery();
connection.Dispose();
command.Dispose();

4 个答案:

答案 0 :(得分:1)

尝试清理一下您的查询或将其粘贴到SSMS上并声明参数,您将找出问题所在。

在您的情况下,您的INSERT语句有一些错误。

  1. 这不是有效的语法VALUES (test_name = @combobox1),而是仅传递参数VALUES (@combobox1)
  2. INSERT子句中指定的值相比,VALUES语句中的列更多,您未提供apte_name的值。在C#代码中,您也需要添加该参数。
  3. 您缺少VALUES子句的右括号

您应该得到这样的结果(未经测试)

string selectQuery = 
@"BEGIN TRANSACTION 
            UPDATE tester SET 
                test_ad = @dateTimePicker1, 
                test_ud = @dt, 
                test_pd = @dt 
            WHERE test_name = @textBox1; 

            INSERT INTO records
            (
                testr_status, 
                testr_name,
                testr_ad,
                testr_ud,  
                testr_pd, 
                apte_name
            )
            VALUES
            (
                @testr_status, 
                @comboBox1,
                @dateTimePicker1,
                @dt, 
                @dt,
                @apte_name
            );

 COMMIT";

答案 1 :(得分:1)

实际的问题是,这是一条很大的无效SQL语句。使用分号分隔语句,如下所示:

"BEGIN TRANSACTION;
INSERT ...;
UPDATE ...;
ETC ...;
COMMIT;"

也就是说,不要将事务语句嵌入查询字符串中。做奥利弗在另一个答案中建议的事情。

答案 2 :(得分:0)

您可以使用SqlTransaction

using (SqlConnection conn = new SqlConnection("Connection String"))
{
    conn.Open();
    SqlTransaction trans;
    trans = conn.BeginTransaction();
    string selectQuery = "your sql query";
    SqlCommand command = new SqlCommand(selectQuery, connection);
    int iResult = command.ExecuteNonQuery();
    if (iResult > 0)
    {
        trans.Commit();
    }else{
        trans.Rollback();
    }
    conn.Close();
}

答案 3 :(得分:-1)

使用@格式化值字符串用于选择查询,并且值块中的语法不正确。

        string selectQuery = @"
BEGIN TRANSACTION
        UPDATE tester SET test_ad = @dateTimePicker1, test_ud = @dt, test_pd = @dt WHERE test_name = @textBox1;
        INSERT INTO records(testr_status, testr_name, testr_ad, testr_ud, testr_pd, apte_name) VALUES(@testr_status, @comboBox1, @dateTimePicker1, @dt, @dt); 
       COMMIT";
相关问题