如何在SQL Server数据库的2表中插入datat? [英] How to insert datat in to 2 table in sql server database?

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

问题描述

我正在使用2表数据库SQL Server,但我不知道如何在SQL Server中插入2表.我正在使用像这样的代码:

Im using 2 table database sql server, but i dont know how to insert 2 table in sql server. Im using like this code like this :

sqltext = "Insert into TbSale(SaleID,CustomerID,Date)values('" & Me.txtSaleID.Text & "', '" & Me.txtCustomerName.Text & "','" & Me.dtDatetime.Text & "'); "
        sqltext &= "Insert into TbSale_Detail(BookTitle,Categories,Author,ISBN, Quantity, UnitPrice) Values('" & Me.txtBookTitle.Text & "','" & Me.cboCategories.Text & "','" & Me.txtAuthor.Text & "','" & Me.txtISBN.Text & "','" & Me.txtQuantity.Text & "','" & Me.txtUnitPrice.Text & "') "
        sqlSellcmd.CommandText = sqltext
        sqlSellcmd.Connection = cn
        RowEffected = sqlSellcmd.ExecuteNonQuery()
        If RowEffected > 0 Then
            Me.ShowItem()
            MsgBox("Insert Data is Successful", MsgBoxStyle.Information)
            sqlSellcmd.Dispose()

推荐答案

我认为使用sqlQuery和一个sqlcommand是比较麻烦的,因为否则,如果sqQuary之一出错,则您都将插入查询卡住了.所以尝试这样使用.
I think it is batter to use to sqlQuery and one sqlcommand because otherwise if one of sqQuary get error your both insert query get stuck. so try to use like this.
string strFirstSql="your first insert sql";
string strSecondSql="your second insert sql";



然后



then

sqlSellcmd.Connection=cn;
sqlSellcmd.CommandText=strFirstSql;
sqlSellcmd.ExecuteNonQuery();
sqlSellcmd.CommandText=strSecondSql;
sqlSellcmd.ExecuteNonQuery();
sqlShellcmd.Dispose();




希望对您有所帮助.




I hope this will help you.


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

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