C#没有将数据保存到SQL Server

时间:2016-07-20 07:20:12

标签: c# sql sql-server winforms

当我用参数替换连接的插入时,INSERT停止保存到数据库。

我用:

Int32 rowsAffected = cmd.ExecuteNonQuery();                          
var totalRecords = (new SqlCommand("SELECT COUNT(*) FROM dbo.EmpInfo", cmd.Connection)).ExecuteNonQuery();

MessageBox.Show("RowsAffected: " + rowsAffected.ToString());
MessageBox.Show("Total Records: " + totalRecords);

但是,RowsAffected的返回值为1,totalRecords的返回值为-1。

if (Status == RegistrationStatus.r_OK)
{
    String query = "INSERT INTO dbo.EmpInfo(EmpYear, EmpStatus, LName, FName, JobTitle, EmpPay, EmpDoB, EmpSex, EmpAddr, EmpCity, EmpState, EmpZIP, EmpCountry, EmpEAddr, EmpTelNo, EmpMobileNo, EmpDate) VALUES (@EmpYear, @EmpStatus, @LName, @FName, @JobTitle, @EmpPay, @EmpDoB, @EmpSex, @EmpAddr, @EmpCity, @EmpState, @EmpZIP, @EmpCountry, @EmpEAddr, @EmpTelNo, @EmpMobileNo, getdate())";

    using (SqlConnection conn = new SqlConnection("Data Source=RB-DESKTOP;Initial Catalog=TimeDB;Persist Security Info=True;User ID=sa;Password=bautista7"))
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            using (SqlTransaction transaction = conn.BeginTransaction())
            {
                cmd.Connection = conn;
                cmd.Transaction = transaction;

                ParameterName = "@EmpYear", Value = EmpYear });
                cmd.Parameters.AddWithValue("@EmpYear", EmpYear);
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpStatus", Value = "Active" });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@LName", Value =   regLname_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@FName", Value =   regFname_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@JobTitle", Value = "NULL" });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpPay", Value =   PayType_cb.SelectedItem.ToString()});
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpDoB", Value =   regDob_dtp.Value.Date  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpSex", Value =   gender });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpAddr", Value =   regAddr_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpCity", Value =   regCity_text.Text  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpState", Value =   regState_text.Text  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpZIP", Value =  regZip_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpCountry", Value = regCountry_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpEAddr", Value = regEmail_text.Text  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpTelNo", Value =   regTel_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpMobileNo", Value =   regMob_text.Text});

                cmd.ExecuteNonQuery();
                transaction.Commit();

3 个答案:

答案 0 :(得分:1)

SqlCommand.ExecuteNonQuery返回受影响的行数,您需要的是执行查询的SqlCommand.ExecuteScalar,并返回第一行的第一列

答案 1 :(得分:0)

您没有在正确的位置使用ExecuteNonQuery,而您需要在其他地方使用ExecuteScalar。此外,rowsAffected将始终为1,因为您只插入一条记录。

if (Status == RegistrationStatus.r_OK)
{
    String query = "INSERT INTO dbo.EmpInfo(EmpYear,EmpStatus,LName,FName,JobTitle,EmpPay,EmpDoB,EmpSex,EmpAddr,EmpCity,EmpState,EmpZIP,EmpCountry,EmpEAddr,EmpTelNo,EmpMobileNo,EmpDate) values(@EmpYear,@EmpStatus,@LName,@FName,@JobTitle,@EmpPay,@EmpDoB,@EmpSex,@EmpAddr,@EmpCity,@EmpState,@EmpZIP,@EmpCountry,@EmpEAddr,@EmpTelNo,@EmpMobileNo,getdate())";
    using (SqlConnection conn = new SqlConnection("Data Source=RB-DESKTOP;Initial Catalog=TimeDB;Persist Security Info=True;User ID=sa;Password=bautista7"))
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            using (SqlTransaction transaction = conn.BeginTransaction())
            {
                cmd.Connection = conn;
                cmd.Transaction = transaction;
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpYear", Value = EmpYear });
                cmd.Parameters.AddWithValue("@EmpYear", EmpYear);
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpStatus", Value = "Active" });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@LName", Value =   regLname_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@FName", Value =   regFname_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@JobTitle", Value = "NULL" });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpPay", Value =   PayType_cb.SelectedItem.ToString()});
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpDoB", Value =   regDob_dtp.Value.Date  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpSex", Value =   gender });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpAddr", Value =   regAddr_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpCity", Value =   regCity_text.Text  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpState", Value =   regState_text.Text  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpZIP", Value =  regZip_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpCountry", Value = regCountry_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpEAddr", Value = regEmail_text.Text  });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpTelNo", Value =   regTel_text.Text });
                cmd.Parameters.Add(new SqlParameter() { ParameterName = "@EmpMobileNo", Value =   regMob_text.Text});

                Int32 rowsAffected = cmd.ExecuteNonQuery();
                transaction.Commit();                        
                var totalRecords = (new SqlCommand("SELECT COUNT(*) FROM dbo.EmpInfo", cmd.Connection)).ExecuteScalar();
                MessageBox.Show("RowsAffected: " + rowsAffected.ToString());
                MessageBox.Show("Total Records: " + totalRecords);
           }
        }
   }
}

答案 2 :(得分:0)

您可以随时剪切n将您的sql粘贴到自己的文件中,然后让QueryFirst生成所有参数代码。您的sql语法将被验证,您不必担心参数的类型。您的维护将被简化,如果您的数据库架构发生了变化,您将立即看到您的sql是否已损坏。