我想通过匹配两个表的相同字段将数据插入表中 [英] i want to insert data into a table by matching same fields of two tables

查看:76
本文介绍了我想通过匹配两个表的相同字段将数据插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码



this is my code

command.CommandText = "insert into BasicDtl_tbl([Nationality],[Post]) values ('" + Nattxt.Text + "','" + apldposttxt.Text + "') WHERE UserName =(select UserName from UserReg)";





i希望通过匹配basicdtl和userreg表中存储的唯一用户名将一些细节输入basicdtl表



i want to enter the some details into a basicdtl table by matching the unique username stored in both basicdtl and userreg table

推荐答案

回复你的评论

In response to your comment
引用:

如果我在显示的文本框中输入用户名值fil表单应用

编写一个查询,通过使用where子句将数据插入表中,如下面的代码



command.CommandText =插入BasicDtl_tbl([国籍],[发布])值('+ Nattxt.Text +','+ apldposttxt.Text +')WHERE UserName = usertxt.Text;



这是写查询的正确方法

if i enter username value into a textbox displayed in fil form application
write a query to insert data into table by using where clause like the following code

command.CommandText = "insert into BasicDtl_tbl([Nationality],[Post]) values ('" + Nattxt.Text + "','" + apldposttxt.Text + "') WHERE UserName =usertxt.Text ";

is this the right way of writing a query



- 你非常接近。正如您已经解决的那样,您不需要连接到UserReg表。我建议你总是使用参数化查询 [ ^ ]使用数据时由用户输入。例如




- you are very close. You don't need the join to table UserReg, as you have worked out. I would advise you to always use Parameterized Queries[^] when using data entered by a user. For example

string sql = "insert into BasicDtl_tbl([Nationality],[Post]) values (?,?) WHERE UserName =?";
// where conn is your connection...
using (OleDbCommand command = new OleDbCommand(SqlString, conn)) 
{
  command.CommandType = CommandType.Text;
  command.Parameters.AddWithValue("Nationality", Nattxt.Text);
  command.Parameters.AddWithValue("Post", apldposttxt.Text);
  command.Parameters.AddWithValue("Post", usertxt.Text);
  command.ExecuteNonQuery();
}



注意我所做的就是用问号(?)替换你的 sql中的文本框并添加行创建参数


Notice all I've done is replace the textboxes in your sql with question marks (?) and added the lines to create the parameters


由于您已经在表中使用了用户名,因此您应该使用update sql:

Since you already have username in the table, you should be using update sql:
sqlcmd.CommandText = "UPDATE BasicDtl_tbl SET nationality = @nationality, post = @post WHERE username = @username";
sqlcmd.Parameters.Add(AddDbParmSpecificValue( "@nationality", nationtxt.Text));
sqlcmd.Parameters.Add(AddDbParmSpecificValue( "@post", posttxt.Text) );
sqlcmd.Parameters.Add(AddDbParmSpecificValue( "@username", usertxt.Text ) );


这篇关于我想通过匹配两个表的相同字段将数据插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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