简体   繁体   English

无效的列名“ textBox.Text”

[英]Invalid column name 'textBox.Text'

I'm using Sql for the first time. 我是第一次使用Sql。 I have a visual C# project and a form filled with text boxes. 我有一个可视的C#项目和一个填充文本框的表单。 When I try to get the information form first and second text box to make a new save in the table, I recieve an error 'Invalid column name 'value from first text box.'' . 当我尝试从信息表的第一个和第二个文本框中获取信息以在表中进行新保存时,我收到一个错误消息'Invalid column name'value from the first text box。''。 Here is the code I think will be needed to help me: 这是我认为需要帮助的代码:

SqlConnection connection = new SqlConnection(global::DiplomaAssignment.Properties.Settings.Default.Database1ConnectionString);
 string sql = "INSERT INTO StudentsTable (First_name,Second_name) values (" + textBox1.Text + ",'" + textBox2.Text + "')";
            SqlCommand executeSql = new SqlCommand(sql, connection);
            connection.Open();
            executeSql.ExecuteNonQuery();
connection.Close(); 

I think the problem is in the sql string syntax, but as I mentioned before I'm new and can't understand the displayed error. 我认为问题出在sql字符串语法中,但是如前所述,我是新手,无法理解显示的错误。

I have modified your sql a bit here. 我在这里修改了你的sql。 I think its better to use string.format instead gluing sting together. 我认为最好使用string.format而不是将st钉粘合在一起。 See if following works: 查看以下作品是否有效:

string sql = "INSERT INTO StudentsTable (First_name,Second_name) values (string.format("{0},{1}",textbox1.text.trim(),textbox2.text.trim()))";

Note that there could be syntax error. 请注意,可能存在语法错误。

Try this ... 尝试这个 ...

string sql = "INSERT INTO StudentsTable (First_name,Second_name) values ('" + textBox1.Text + "','" + textBox2.Text + "')";
                                                                         ^                     ^ missing single quotes

You missed to put textBox1.Text in single qoutes. 您错过了将textBox1.Text放入单个qoutes的操作。

string sql = "INSERT INTO StudentsTable (First_name,Second_name) values ('" + textBox1.Text + "','" + textBox2.Text + "')"

It should work this way. 它应该以这种方式工作。

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

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