当我尝试运行以下代码时,出现错误。这可能是什么问题?

protected void Button1_Click(object sender, EventArgs e)
 {
        SqlConnection cnn = new SqlConnection("server=.; database=YEDEK; Integrated Security=True; ");
        cnn.Open();
        SqlCommand cmd = cnn.CreateCommand();
        cmd.CommandText = "insert Personel (Name,Surname,Tel) values  ('"+txtName.Text+"','"+ txtSurname.Text+"','"+txtTel.Text+"')  ";
        SqlParameter p1 = new SqlParameter("txtName.Text", SqlDbType.NVarChar);
        p1.Value = "txtName.Text";
        cmd.Parameters.Add(p1);
        SqlParameter p2 = new SqlParameter("txtSurname.Text", SqlDbType.NVarChar);
        p2.Value = "txtSurname.Text";
        cmd.Parameters.Add(p2);
        SqlParameter p3 = new SqlParameter("txtTel.Text", SqlDbType.Char);
        p3.Value = "txtTel.Text";
        cmd.Parameters.Add(p3);
        cmd.ExecuteNonQuery();
        cnn.Close();

 }


这是我的错误信息:

 Incorrect syntax near '.'.
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 Exception Details:  System.Data.SqlClient.SqlException: Incorrect syntax near '.'.

 Source Error:

 Line 44:             //cmd.Parameters.Add(p3);
 Line 45:
 Line 46:             cmd.ExecuteNonQuery();
 Line 47:         //}
 Line 48:         //catch (SqlException ex)

最佳答案

您的参数语法不正确。

适当的参数如下所示:

 new SqlParameter("@SomeParamName", SqlDbType.VarChar)


似乎您正在尝试将控件中的值直接插入参数中。在这种情况下,您可以这样做:

  var param = new SqlParameter("@Name", SqlDbType.VarChar);
  param.Value = txtName.Text;


参数名称应与您的存储过程定义匹配。

09-28 13:34