c# - 通过事件处理程序将值插入表中

标签 c# mysql visual-studio-2008

我已将由 sql server 创建的本地数据库连接到我在 visual studio (C#) 中的项目。现在我希望将用户在文本字段中给出的数据输入到我的数据库中。这是我尝试做的事情

    private void Button_AddCustomer_Click(object sender, EventArgs e)
    {
         try
         {
             //SqlConnection objsqlconn = new SqlConnection(conn);
             SqlConnection myConnection = new SqlConnection("Data Source=SHIRWANIPC;" + 
"Initial Catalog=TEST DATABASE;" + "Integrated Security=True");
             myConnection.Open();
             SqlCommand objcmd = new SqlCommand("INSERT INTO 
Customer(PhoneNumber,MobileNumber,Address) VALUES (a, b, c)", myConnection);
             objcmd.ExecuteNonQuery();
         }
         catch(SqlException ex)
         {
              MessageBox.Show(ex.ToString());
         }
     }

它抛出一个异常说明无效的列名a,无效的列名b,无效的列名c。问题是什么以及如何使用插入查询将用户输入到我的数据库中?我正在使用 visual studio C#,本地数据库是使用 ms sql 创建的。

最佳答案

替换

VALUES (a, b, c)

VALUES (' + textBox1.value + (other text area) + ')'

无论如何在查询之前检查输入!

好的

SqlCommand objcmd = new SqlCommand("INSERT INTO Customer(PhoneNumber,MobileNumber,Address) VALUES ('" + PhoneNumber.Text + "', '" + MobileNumber.Text + "', '" + Address.Text + "')", myConnection); 

关于c# - 通过事件处理程序将值插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22094154/

相关文章:

c# - 系统.Net.WebException : The request was aborted: Could not create SSL/TLS secure channel

c# - 无法转换异常类型的对象

php - 我无法将数据添加到我的 mysql 数据库中的表中

mysql - 使用 MySql 条件在表之间传递值

c# - 使用三元运算构建字符串

c# - 将不同的方法/函数传递给后台工作人员的 DoWork 事件

mysql - 为什么需要在数据库中显式指定外键和引用?

c++ - C++ Visual Studio 2008 中未声明的标识符

c - 这个字符应该是无符号的吗?

winforms - 自定义控件和 Visual Studio 2008 SP1