插入编码错误 [英] error in insert coding

查看:72
本文介绍了插入编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我是新手,我的朋友发现了一些编码错误.
我无法理解问题所在.

sorry i am a newbie and my friend found out some coding error.
i am not able to understand what the problem is.

SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\Projects\labelnameinsecondformtry\labelnameinsecondformtry\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            try
            {
                cmd.CommandText = "Insert into Table2 values (username)";
                cmd.Parameters.Add("@username", SqlDbType.VarChar).Value =textBox1.Text;
                cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
                MessageBox.Show("hello welcome");
            }



错误是出现新的executenonquery并说



the error is comming new executenonquery and saying that

Invalid column name ''username'' but username column us there in my database whats the real problem in this coding

推荐答案

您的commandText不正确.插入语句的正确语法为:
Your commandText is incorrect. The proper syntax for an insert statement would be:
INSERT INTO Table2 (username) VALUES (@username)


这是有关INSERT语句的资源: TSQL INSERT [


Here is a resource on the INSERT statement: TSQL INSERT[^]


这篇关于插入编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆