繁体   English   中英

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

[英]Inserting data from textbox into SQL Server database

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

这是我的代码

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM