i am using pubs Database and Two Tables
1. A (Table name)
it contains two field
1 id int,
2 name varchar(10)
2. B (Table Name) - is is empty table
it contains two field
1 name varchar(10),
This is My problem using SqlCommandBuilder
string a = "server=.;database=pubs;uid=sa;pwd=jk;";
string b ="select name from A where id=1;
SqlConnection con = new SqlConnection(a);
SqlDataAdapter da = new SqlDataAdapter(b, con);
Dataset ds = new DataSet();
da.Fill(ds,"Temp"); ------------------Dataset 1
string c ="select * from B;
SqlConnection con1 = new SqlConnection(a);
SqlDataAdapter da1 = new SqlDataAdapter(c, con);
Dataset ds1 = new DataSet();
da1.Fill(ds1,"Vemp"); ------------------------- Dataset 2
SqlCommandBuilder buil = new SqlCommandBuilder(da1);
da1.Update(ds,"Temp");
is is possible to insert A table to B using Command Builder
Loading
Master BillaPosted Sep 8, 2009, 12:24 PM
Yes, just write few lines of code to do this.Your code like be this way
private void button3_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = "connection string";
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "insert into tableA select C1,C2 FRom TableB;";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
}
Thank youKirtan PatelPosted Sep 8, 2009, 8:00 AM
dont forget to mark " Do you like answer" it will give me some credits :)
you can simply do it will Just one Sql Query that will copy A's Content to Table B
but make Sure That Both Table Having Same Stucture(ie Same Columns)
see below code
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=test;Integrated Security=True");
con.Open();
SqlCommand comm = new SqlCommand("insert into B select * from A", con);
comm.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Inserted Into Another Table ");
}
happy coding :)