绑定到sqlServer2008r2时出现问题

问题描述:

嗨.

我在下面编写了代码:

Hi.

I have written a code below:

protected void SubmitButton(object sender, EventArgs e)
{
    SqlConnection Conn = new SqlConnection();

    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["PersonTest"].ConnectionString;
    SqlCommand Cmd = new SqlCommand();
    Cmd.CommandText = "INSERT INTO Perosn [Name,Family] Values (@Name,@Family)";

    Cmd.Connection = Conn;
    Conn.Open();
    Cmd.ExecuteNonQuery();
    Conn.Close();
}



但是在调试解决方案时,会出现此异常:

SqlException未由用户代码处理.
名称,家庭"附近的语法不正确.




but when debug the solution this exception appears :

SqlException was unhandled by user code.
Incorrect syntax near ''Name,Family''.


what shall i doooo!!!?

您尚未将值传递给参数@ Name,@ Family

因此请使用
将参数值传递给它

Cmd.CommandText ="INSERT INTO Perosn [Name,Family]值(@ Name,@ Family)";
Cmd.Parameters.AddWithValue(" @ Name,txtName.Text);
Cmd.Parameters.AddWithValue("@ Family",txtFamily.Text);

Cmd.Connection = Conn;
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
you have not passed values to the argument @Name,@Family

so pass argument values to it using


Cmd.CommandText="INSERT INTO Perosn [Name,Family] Values (@Name,@Family)";
Cmd.Parameters.AddWithValue("@Name", txtName.Text);
Cmd.Parameters.AddWithValue("@Family", txtFamily.Text);

Cmd.Connection = Conn;
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();


嗨..
您没有传递参数@ Name,@ Family ...来获取数据...
Orelse在您的命令中希望表名的拼写错误拼写为 Perosn 而不是 Person 我认为..
所以看看吧.
Hi..
You have either not pass the parameters @Name,@Family... To Fetch the data...
Orelse in your command hope the table name spelling is misspelled as Perosn instead of Person i think..
So check it out..



试试这个:
Hi,
Try this:
protected void SubmitButton(object sender, EventArgs e)
{
     SqlConnection Conn = new SqlConnection();
 
     Conn.ConnectionString = ConfigurationManager.ConnectionStrings["PersonTest"].ConnectionString;
     SqlCommand Cmd = new SqlCommand();
     Cmd.CommandText = "INSERT INTO Perosn (Name, Family) Values (@Name,@Family)";
     //probem was(Name, Family) brackets and you din't added the parameters.
     Cmd.Parameters.AddWithValue("@Name", txtName.Text);
     Cmd.Parameters.AddWithValue("@Family", txtFamily.Text);
     Cmd.Connection = Conn;
     Conn.Open();
     Cmd.ExecuteNonQuery();
     Conn.Close();                
}




--Amit




--Amit