如何在表格中插入值 [英] how to insert value in tabel

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

问题描述


我正在一个asp.net应用程序上工作..
我在使用ms sql时遇到一个问题
我想在一个表中插入值,就像我在另一个表中插入值..


就像我有一个表Test1,其中包含字段UID,fname,Lname
另一个表格Test2包含字段UID ..
我希望在Test2中插入记录时增加Test2中的UID值..


i m working on one asp.net application..
i m facing one problem while working with ms sql
i want to insert the values in one table as i insert the values in another tabel..


like i have one tabel Test1 which contain field UID,fname,Lname
and another tabel Test2 which contain field UID ..
i want UID values in Test2 get incremented as i insert in records in Test1..

推荐答案

您需要触发器...

阅读:
MSDN:触发器 [
You need triggers...

Read:
MSDN: Trigger[^]

Alternatively, if you need to add something else (you need some manual work) then you can use a stored procedure where you can have both inserts.
OR
have a transaction and commit once both the tables are updated.

Reading Good SQL book will be of great help.


尝试以下两个查询


对于table1

字符串str ==插入测试值(" + txtbox1.text +'',""+ txtbox1.text +"''....);
sqlcommand com = new sqlcommand(str,connection);
com.executenonquery();


对于表2

首先获取Uid的值

字符串str1 =从table2中选择Max(UID)";
sqlcommand com1 =新的sqlcommand(str1,connection);
int i =(convert.toint32(com.executeScaler().tostring()));
i = i + 1;

然后执行插入查询;
try following two query


For table1

String str=Insert into test values(''"+txtbox1.text+"'',''"+txtbox1.text+"''....);
sqlcommand com=new sqlcommand(str,connection);
com.executenonquery();


For table 2

first retrive the value of Uid

String str1="Select Max(UID) from table2";
sqlcommand com1=new sqlcommand(str1,connection);
int i=(convert.toint32(com.executeScaler().tostring()));
i=i+1;

then execute insert query;


这篇关于如何在表格中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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