数据未插入数据库表

时间:2014-10-23 13:54:27

标签: c# asp.net sql-server

string constr = "Data Source =. ;Initial Catalog =reg ;Integrated Security=true;";
SqlConnection conn = new SqlConnection(constr);
conn.Open();
string sql = "Insert Into Course Values(@CustomerFName,@CustomerLName,@Email ,@PhoneNo ,@CourseName);";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@CustomerFName", txthrfn.Text);
cmd.Parameters.AddWithValue("@CustomerLName", txthrln.Text);
cmd.Parameters.AddWithValue("@Email", txthrem.Text);
cmd.Parameters.AddWithValue("@PhoneNo", txthrmob.Text);
cmd.Parameters.AddWithValue("@CourseName", ddlhr.SelectedItem.ToString());

cmd.ExecuteNonQuery();
conn.Close();

1 个答案:

答案 0 :(得分:1)

我尝试过类似的事情:

    string constr = "Data Source =. ;Initial Catalog =reg ;Integrated Security=true;";
    SqlConnection conn = new SqlConnection(constr);
    try
    {
        conn.Open();
    }
    catch (Exception err)
    { MessageBox.Show(err.Message); }
    string sql = "Insert Into Course(CustomerFName,CustomerLName,Email,PhoneNo,CourseName) Values(@CustomerFName,@CustomerLName,@Email,@PhoneNo,@CourseName)";
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.AddWithValue("@CustomerFName", txthrfn.Text);
    cmd.Parameters.AddWithValue("@CustomerLName", txthrln.Text);
    cmd.Parameters.AddWithValue("@Email", txthrem.Text);
    cmd.Parameters.AddWithValue("@PhoneNo", txthrmob.Text);
    cmd.Parameters.AddWithValue("@CourseName", ddlhr.SelectedItem.ToString());

    try { cmd.ExecuteNonQuery(); }
    catch (Exception err)
    { MessageBox.Show(err.Message); }

    try { conn.Close(); }
    catch (Exception err)
    { MessageBox.Show(err.Message); }

至少那样你就会知道什么是错的。

注意:暂时不能评论您的问题(代表不够)。