给个例子:点击Button按钮时将表A的数据全部导到表B中,并把新复制到表B的数据显示在DataGrid中。
private void Button1_Click(Object sender,EventArgs e)
{
string constr="database=abc;data source=(local);user id=sa;password=abc";
SqlConnection con=new SqlConnection(constr);
SqlCommand cmd=new SqlCommand();
cmd.Connection=con;
cmd.CommandText="create table B(ID char(10) not null,Pic image null,other varchar(50) null)";
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch{}
cmd.CommandText="insert into B select * from A";
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch{}
cmd.CommandText="select * from B";
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
DataTable dt=new DataTable();
try
{
da.Fill(dt);
}
catch{}
this.DataGrid1.DataSource=dt;
}