C# 将文本框中的数据插入SQL Server数据库

C# 将文本框中的数据插入SQL Server数据库,c#,sql-server,C#,Sql Server,我不知道问题出在哪里。我试图将文本框中的数据插入数据库,但出现如下错误 这是我的密码 private void but_Add_Click(object sender, EventArgs e) { String query = "INSERT INTO Tbl_Cashier (FName, MName, LName, Address, ContactNo, Email, Age, Gender, Password, role) VALUES (@FName, @MName, @LNa

我不知道问题出在哪里。我试图将文本框中的数据插入数据库,但出现如下错误

这是我的密码

private void but_Add_Click(object sender, EventArgs e)
{
    String query = "INSERT INTO Tbl_Cashier (FName, MName, LName, Address, ContactNo, Email, Age, Gender, Password, role) VALUES (@FName, @MName, @LName, @Address, @ContactNo, @Email, @Age, @Gender, @Password, @role)";

    using (SqlConnection connection = new SqlConnection(connectionString1))
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        connection.Open();

        command.Parameters.AddWithValue("@FName", txb_Fname);
        command.Parameters.AddWithValue("@MName", txb_Mname);
        command.Parameters.AddWithValue("@LName", txb_Lname);
        command.Parameters.AddWithValue("@Address", txb_Address);
        command.Parameters.AddWithValue("@ContactNo", txb_ContactNo);
        command.Parameters.AddWithValue("@Email", txb_Email);
        command.Parameters.AddWithValue("@Age", txb_Age);
        command.Parameters.AddWithValue("@Gender", txb_Gander);
        command.Parameters.AddWithValue("@Password", txb_Password);
        command.Parameters.AddWithValue("@role", txb_Role);

        command.ExecuteNonQuery();
        command.ExecuteScalar();
        connection.Close();
    }
}
我得到的错误是:

System.Data.dll中发生类型为“System.ArgumentException”的未处理异常

其他信息:不存在从对象类型System.Windows.Forms.TextBox到已知托管提供程序本机类型的映射


您需要在控件末尾添加.Text

command.Parameters.AddWithValue("@FName", txb_Fname.Text);
command.Parameters.AddWithValue("@MName", txb_Mname.Text);
command.Parameters.AddWithValue("@LName", txb_Lname.Text);
command.Parameters.AddWithValue("@Address", txb_Address.Text);
command.Parameters.AddWithValue("@ContactNo", txb_ContactNo.Text);
command.Parameters.AddWithValue("@Email", txb_Email.Text);
command.Parameters.AddWithValue("@Age", txb_Age.Text);
command.Parameters.AddWithValue("@Gender", txb_Gander.Text);
command.Parameters.AddWithValue("@Password", txb_Password.Text);
command.Parameters.AddWithValue("@role", txb_Role.Text);

您需要在控件末尾添加.Text

command.Parameters.AddWithValue("@FName", txb_Fname.Text);
command.Parameters.AddWithValue("@MName", txb_Mname.Text);
command.Parameters.AddWithValue("@LName", txb_Lname.Text);
command.Parameters.AddWithValue("@Address", txb_Address.Text);
command.Parameters.AddWithValue("@ContactNo", txb_ContactNo.Text);
command.Parameters.AddWithValue("@Email", txb_Email.Text);
command.Parameters.AddWithValue("@Age", txb_Age.Text);
command.Parameters.AddWithValue("@Gender", txb_Gander.Text);
command.Parameters.AddWithValue("@Password", txb_Password.Text);
command.Parameters.AddWithValue("@role", txb_Role.Text);

使用
txb\u name.Text
。此外,您还使用
ExecuteNonQuery()
ExecuteScalar()
执行了两次查询。停止标量执行。哦,好吧^^我会尽力感谢你^我明白了,我几乎要问你为什么它变成了两次。再次感谢^^。也感谢您使用参数化SQL:d您不需要同时运行
ExecuteNonQuery
ExecuteScalar
——如果同时运行这两个命令,则会插入两次数据。只需使用
ExecuteNonQuery()
(这是运行
INSERT
DELETE
UPDATE
——不返回结果集的命令)使用
txb\u name.Text
。此外,您还使用
ExecuteNonQuery()
ExecuteScalar()
执行了两次查询。停止标量执行。哦,好吧^^我会尽力感谢你^我明白了,我几乎要问你为什么它变成了两次。再次感谢^^。也感谢您使用参数化SQL:d您不需要同时运行
ExecuteNonQuery
ExecuteScalar
——如果同时运行这两个命令,则会插入两次数据。只需使用
ExecuteNonQuery()
(这是运行
INSERT
DELETE
UPDATE
的命令-不返回结果集的命令)