如何只将选定的数据插入表? [英] How to insert only selected data to table?
本文介绍了如何只将选定的数据插入表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在sql中有一个表登录,它包含很多字段(例如fname,lname,Password,address,Username等)。我只需要向表中插入一些数据(用户名和密码)。我使用C#中的代码
SqlCommand cmd = new SqlCommand( 插入登录值(@UserName),con);
cmd.Parameters.AddWithValue( @ UserName,txtUserName.Text);
cmd.Parameters.AddWithValue( @ Password,txtPassword.Text);单击提交按钮
显示错误消息:
列名或提供的值数与表定义不匹配
请更正问题。
解决方案
您使用的SQL不正确,应该像下一个:
SqlCommand cmd = new SqlCommand( 插入登录(用户名,密码)值(@UserName,@ Password),con);
尝试这个..
如果您想插入特定列USE列名称
SqlCommand cmd = new SqlCommand( insert into login(Username,Password)值(@ UserName,@ Password),con);
cmd.Parameters.AddWithValue( @ UserName,txtUserName。文本跨度>);
cmd.Parameters.AddWithValue( @ Password,txtPassword。文本);
如果您只想插入特定的列数据,那么您还必须在查询中写入它...
SqlCommand cmd = new SqlCommand( 插入登录(用户名,密码)值(@ UserName,@ Password),con);
cmd.Parameters.AddWithValue( @ UserName,txtUserName.Text);
cmd.Parameters.AddWithValue( @ Password,txtPassword.Text);
Hi,
I have a table login in sql it contains lots of fields(for example fname,lname,Password,address,Username etc). I need to insert some data only to the table(Username and Password). I use the code in C#
SqlCommand cmd = new SqlCommand("insert into login values(@UserName)", con);
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
on clicking submit button it shows an error message:
"Column name or number of supplied values does not match table definition"
Please correct the problem.
解决方案
Your SQL used is incorrect, and should be like the next one:
SqlCommand cmd = new SqlCommand("insert into login (Username, Password) values(@UserName, @Password)", con);
Try this..
IF You want to insert specific columns USE Columns name
SqlCommand cmd = new SqlCommand("insert into login (Username, Password) values (@UserName,@Password)", con); cmd.Parameters.AddWithValue("@UserName", txtUserName.Text); cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
IF You want to insert specific columns data only then you have to write it also in query like that...
SqlCommand cmd = new SqlCommand("insert into login (Username, Password) values (@UserName,@Password)", con); cmd.Parameters.AddWithValue("@UserName", txtUserName.Text); cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
这篇关于如何只将选定的数据插入表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文