I am a real novice in c#.
I have seen many questions like this over here. But, on trying, I could not get my code.
I want all the columns of a row to be retrieved and then transfer it to another table of same database.
So basically, retrieve data from table1
and copy it to the respective columns of table2
.
I have tried this much code. And I am confused as how to call directly check string instead of defining each column in str1 string in the " ".
string check = "select * from custdetails where billid = @billid ";
SqlCommand cmd1 = new SqlCommand(check , conn);
cmd1.Parameters.AddWithValue("@billid", billid);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(ds);
// string str1;
int count = ds.Tables[0].Rows.Count;
if (count > 0)
{
string str1 = ds.Tables[0].Rows[0][""].ToString();
}
I really dont have much idea about c#. Thanks for help.
You can do so by using :
INSERT INTO table2 (SELECT * FROM table1 WHERE billid = @billid)
the above is gud only when table1
& table2
had same structure. In case both table has different structure you need to change SELECT *
to SELECT [COLUMNS ,]...
One you are done Copying the Data then you can carry on with reading data into you application
string check = "select * from custdetails where billid = @billid ";
SqlCommand cmd1 = new SqlCommand();
// use the command object to copy table first....
cmd1.Connection = conn;
cmd1.CommandText = "INSERT INTO table2 (SELECT * FROM table1 WHERE billid = @billid)";
cmd1.ExecuteNonQuery();
//then continue doing the normal work
cmd1.CommandText = check;
cmd1.Parameters.AddWithValue("@billid", billid);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd1);
da.Fill(ds);
// string str1;
int count = ds.Tables[0].Rows.Count;
if (count > 0)
{
string str1 = ds.Tables[0].Rows[0][""].ToString();
}
You can achieve it by the following line.
INSERT INTO targettable
SELECT * FROM custdetails WHERE billid = @billid;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.