C#中怎么把一张表中的某些行复制到另一张表?

时间:2022-07-22 04:31:38
RT,要复制到的表结构跟原表一样的,已创建好。
如何把一张表中的满足条件的某些行复制到另一张表呢?
就是insert into table2 SELECT * FROM table1 where XXX,怎么用C#代码实现呢?

12 个解决方案

#1


写好SQL语句之后,在C#中建立连接执行

#2


public void CreateMyOleDbCommand(string myExecuteQuery, string myConnectionString) 
{
   OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   OleDbCommand myCommand = new OleDbCommand(myExecuteQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();
}

#3


myExecuteQuery就是你的SQL语句,myConnectionString就是连接串,使用数据库连接向导向导可以得到标准的连接串

#4


SELECT *  into table2 FROM table1 where XXX

#5


表结构要一致
不一致的话需要人为对齐。

比如:table1  id sname
      table2  id fullname password

select id,fullname as sname into table1 from table2 where ...

#6


使用Command对象的ExecuteNonQuery()方法执行你的语句即可

#7


使用Command对象的ExecuteNonQuery()方法执行你的语句即可

#8


软件开发与技术交流群40271625

#9


存储过程里写~~

#10


把table1里面的行clone,再加到table2里面

#11


建议用存储过程来实现

#12


这个存储过程没有完成,还有原始表的列属性没有取出,希望能有人帮助完成,先谢了。
create proc [dbo].[Copytable](@OldTable varchar(20))
as
create table #temp(ID int)
declare @ExecSql varchar(1000)
select @ExecSql=''
declare @name varchar(20)
declare @type int
declare @length char(2)
declare @TypeName varchar(20)
declare tablename cursor for select name,xtype,length from syscolumns where id in( select id from sysobjects where name='t_ortherbadysdjb' and xtype='u' )and Name !='id'
open tablename
fetch next from tablename into @name,@type,@length
while @@fetch_status=0
begin
select @TypeName=name from systypes where xtype=@type
--int类型与字符类型的写法不统一
if @TypeName='int'
begin
select @ExecSql=' alter table #temp add '+@name+' '+@TypeName
exec(@ExecSql)
end
else
begin
select @ExecSql=' alter table #temp add '+@name+' '+@TypeName+'('+@length+')'
exec(@ExecSql)
end
fetch next from tablename into @name,@type,@length
end
close tablename
deallocate tablename
select * from #temp
drop table #temp

#1


写好SQL语句之后,在C#中建立连接执行

#2


public void CreateMyOleDbCommand(string myExecuteQuery, string myConnectionString) 
{
   OleDbConnection myConnection = new OleDbConnection(myConnectionString);
   OleDbCommand myCommand = new OleDbCommand(myExecuteQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.ExecuteNonQuery();
   myConnection.Close();
}

#3


myExecuteQuery就是你的SQL语句,myConnectionString就是连接串,使用数据库连接向导向导可以得到标准的连接串

#4


SELECT *  into table2 FROM table1 where XXX

#5


表结构要一致
不一致的话需要人为对齐。

比如:table1  id sname
      table2  id fullname password

select id,fullname as sname into table1 from table2 where ...

#6


使用Command对象的ExecuteNonQuery()方法执行你的语句即可

#7


使用Command对象的ExecuteNonQuery()方法执行你的语句即可

#8


软件开发与技术交流群40271625

#9


存储过程里写~~

#10


把table1里面的行clone,再加到table2里面

#11


建议用存储过程来实现

#12


这个存储过程没有完成,还有原始表的列属性没有取出,希望能有人帮助完成,先谢了。
create proc [dbo].[Copytable](@OldTable varchar(20))
as
create table #temp(ID int)
declare @ExecSql varchar(1000)
select @ExecSql=''
declare @name varchar(20)
declare @type int
declare @length char(2)
declare @TypeName varchar(20)
declare tablename cursor for select name,xtype,length from syscolumns where id in( select id from sysobjects where name='t_ortherbadysdjb' and xtype='u' )and Name !='id'
open tablename
fetch next from tablename into @name,@type,@length
while @@fetch_status=0
begin
select @TypeName=name from systypes where xtype=@type
--int类型与字符类型的写法不统一
if @TypeName='int'
begin
select @ExecSql=' alter table #temp add '+@name+' '+@TypeName
exec(@ExecSql)
end
else
begin
select @ExecSql=' alter table #temp add '+@name+' '+@TypeName+'('+@length+')'
exec(@ExecSql)
end
fetch next from tablename into @name,@type,@length
end
close tablename
deallocate tablename
select * from #temp
drop table #temp