i want to copy all the data from one table to another in database on button click event. here is the code.
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand(" Insert into Consumer(ConsumerNo,ConsumerName,StreetNo,PhaseName,SectorName,PlotSize,DateofConnection,PropertyType,Diameter,Status) select ConsumerNo,ConsumerName,Street,Phase,Sector,PlotSize,DateofConnection,PropertyType,Dia,Status From Table", con);
cmd.ExecuteNonQuery();
}
it gives me the following error:
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'Table'.
please help me out.
Loading
Posted Jun 7, 2011, 1:37 AM
protected void Button1_Click(object sender, EventArgs e).dbo.", con);
{
con.Open();
cmd = new SqlCommand(" Insert into Consumer(ConsumerNo,ConsumerName,StreetNo,PhaseName,SectorName,PlotSize,DateofConnection,PropertyType,Diameter,Status)
select ConsumerNo,ConsumerName,Street,Phase,Sector,PlotSize,DateofConnection,PropertyType,Dia,Status From
cmd.ExecuteNonQuery();
}
Change the blue highlighted lines as per your source database details.Or ppost the source db name, schema, and table name details.
saifullah khanPosted Jun 7, 2011, 1:33 AM
SqlConnection con = new SqlConnection("Data Source=EMINENCE-PC\\SQLEXPRESS;Initial Catalog=pda_rtc;Integrated Security=True");
SqlCommand cmd;
SqlDataReader dr;
SqlDataAdapter ad;
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand(" Insert into Consumer(ConsumerNo,ConsumerName,StreetNo,PhaseName,SectorName,PlotSize,DateofConnection,PropertyType,Diameter,Status) select ConsumerNo,ConsumerName,Street,Phase,Sector,PlotSize,DateofConnection,PropertyType,Dia,Status From Table", con);
cmd.ExecuteNonQuery();
}
and i am copying from one table to another in same database.
i have also apply the method which you told but it gave the same error.
Posted Jun 7, 2011, 1:25 AM
cmd = new SqlCommand(" Insert into Consumer(ConsumerNo,ConsumerName,StreetNo,PhaseName,SectorName,PlotSize,DateofConnection,PropertyType,Diameter,Status) select ConsumerNo,ConsumerName,Street,Phase,Sector,PlotSize,DateofConnection,PropertyType,Dia,Status From Table", con);
cmd.ExecuteNonQuery();
When you're accessing the data from one database to another you've to specify the databasename.schema/username.table.
so try like this
select ConsumerNo,ConsumerName,Street,Phase,Sector,PlotSize,DateofConnection,PropertyType,Dia,Status From databasename.dbo.table