使用c#向多个表中插入一些数据
本文关键字:插入 数据 使用 | 更新日期: 2023-09-27 18:09:50
我试图一次将一些数据插入2个表中,但我的代码遇到的问题是…我一次只能插入一个表。如果我注释掉第一个插入语句,那么第二个插入将工作,反之亦然。
我的代码
SqlCommand cmd = new SqlCommand("select Name from MainTable where Name= '" + Name+ "' ", sqlcon);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblmsg.Text = "We are already have this Name" + Name;
}
else
{
dr.Close();
sqlcmd.CommandText = "INSERT INTO Table1(Name, Address) VALUES(@Name, @Address)";
sqlcmd.CommandText = "INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";
sqlcmd.Parameters.Clear();
sqlcmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
sqlcmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;
sqlcmd.Parameters.Add("@Company", SqlDbType.VarChar).Value = Company
sqlcmd.Parameters.Add("@Address2", SqlDbType.VarChar).Value = Address2;
sqlcmd.Connection = sqlcon;
sqlcmd.ExecuteNonQuery();
DV_NameAdd.ChangeMode(DetailsViewMode.ReadOnly);
sqlcon.Close();
}
sqlcon.Close();
您正在设置同一个SqlCommand的命令文本
sqlcmd.CommandText = "INSERT INTO Table1(Name, Address) VALUES(@Name, @Address)";
sqlcmd.CommandText = "INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";
只执行最后一个,因为它已经取代了第一个。可能需要执行第一个命令,清除参数集合,设置新文本,然后重新执行,或者创建一个单独的命令
sqlcmd.CommandText = "INSERT INTO Table1(Name, Address) VALUES(@Name, @Address)";
sqlcmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
sqlcmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = Address;
sqlCmd.ExecuteNonQuery();
sqlCmd.Parameters.Clear();
sqlcmd.CommandText = "INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";
sqlcmd.Parameters.Add("@Company", SqlDbType.VarChar).Value = Company
sqlcmd.Parameters.Add("@Address2", SqlDbType.VarChar).Value = Address2;
sqlCmd.ExecuteNonQuery();
顺便说一下,第一个SELECT是非常危险的。您还应该为
您正在覆盖您的原始SqlCommand,这就是为什么一次只有一个工作。SqlCommand一次只运行一个命令。它不像您的代码所期望的那样接受额外版本的CommandText。如果要同时运行两个或更多命令,则必须创建一个以分号分隔的CommandText,如下所示:
sqlcmd.CommandText =
"INSERT INTO Table1(Name, Address) VALUES(@Name, @Address);" +
"INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";
请注意两个命令之间的分号(;)。你的参数名是唯一的,所以这里应该没问题。
有时您可以在两个SQL语句之间使用分号来同时执行两个语句。看看是否改变这个:
sqlcmd.CommandText = "INSERT INTO Table1(Name, Address) VALUES(@Name, @Address)";
sqlcmd.CommandText = "INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";
:
sqlcmd.CommandText = "INSERT INTO Table1(Name, Address) VALUES(@Name, @Address);" +
"INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";
要考虑的另一件事是将逻辑推入insert
查询并消除竞争条件,如下所示:
public bool InsertEntry( string name , string addr1 , string company , string addr2 )
{
const string QUERY = @"
begin transaction
declare @success bit
insert dbo.table_1
select @name , @addr1
where not exists ( select *
from dbo.main_table
where name = @name
)
insert dbo.table_2
select @company , @addr2
where @@rowcount = 1
and not exists ( select *
from dbo.main_table
where name = @name
)
set @success = case when @@ROWCOUNT > 0 then 1 else 0 end
if ( @success = 1 )
begin
commit transaction
end
else
begin
rollback transaction
end
select @success
" ;
bool success = false ;
using ( SqlConnection connection = new SqlConnection( "Server=(local);Database=sandbox;Trusted_Connection=True;"))
using ( SqlCommand command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = QUERY ;
command.Parameters.Add( "@name" , SqlDbType.VarChar ).Value = name ;
command.Parameters.Add( "@addr1" , SqlDbType.VarChar ).Value = addr1 ;
command.Parameters.Add( "@company" , SqlDbType.VarChar ).Value = company ;
command.Parameters.Add( "@addr2" , SqlDbType.VarChar ).Value = addr2 ;
object returnedValue = command.ExecuteScalar() ;
if ( returnedValue is bool )
{
success = (bool) returnedValue ;
}
}
return success ;
}