我试图一次将一些数据插入到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();
答案 0 :(得分:4)
您正在覆盖原始的SqlCommand,这就是为什么一次只能运行一个。 SqlCommand一次只运行一个命令。它不接受您的代码似乎期望的其他版本的CommandText。如果要同时运行两个或更多命令,则必须创建一个以分号分隔的CommandText,如下所示:
sqlcmd.CommandText =
"INSERT INTO Table1(Name, Address) VALUES(@Name, @Address);" +
"INSERT INTO Table2(Company, Address2) VALUES(@Company, @Address2)";
注意两个命令之间的分号(;)。您的参数名称是唯一的,因此您应该没问题。
答案 1 :(得分:4)
您正在设置相同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非常危险。您还应该为该命令使用参数
答案 2 :(得分:1)
有时,您可以在两个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)";
适合你。
答案 3 :(得分:0)
要考虑的另一件事是将逻辑推入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 ;
}