How i can select table1 and insert into table2?

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

Hello NG

i use Microsoft SQL server
and my problem is how i can select table1 into the dataset and insert this
datas into table2 (backup table).

Has someone a idea or a code example?

Thanks

Michael
 
Michael,

It would be easy if you could change the RowState property on the
DataRow, so that you could simulate the adding of the rows by flipping the
flag for the state. However, this is not the case.

You can duplicate the structure of table 1, and then cycle through all
of the rows in table 1, adding them to table 2. Then, you can create a data
adapter for table 2 and then call the Update method on it.

Hope this helps.
 
Hello Nicholas an thank you for your answer.

I'm not sure:

I have a select statement for tblFiBuErfassen

and now i can copy this dataadapter.....


Here i have a example is that the correct way?
---------------------------------------------------

private void FiBuBuchungBuchen()
{
MessageBox.Show("I'm here...");
// new dataadapter
da2 = new SqlDataAdapter();

SqlCommand cmdInsert= sqlCN.CreateCommand();
cmdInsert.CommandType = CommandType.Text;
cmdInsert.CommandText = "INSERT INTO tblFiBuBuchung " +
"(FiBuBuchungBuchungsnummer,
FiBuBuchungBuchungsdatum,FiBuBuchungBuchungstypID,"+
"FiBuBuchungKontenplanID, FiBuBuchungMWSTID, FiBuBuchungBelegnummer,
FiBuBuchungBuchungstext, "+
"FiBuBuchungBetragSoll, FiBuBuchungBetragHaben, FiBuBuchungName,
FiBuBuchungMandantID,FiBuBuchungUserID) " +
"VALUES(@FiBuBuchungBuchungsnummer,
@FiBuBuchungBuchungsdatum,@FiBuBuchungBuchungstypID,"+

"@FiBuBuchungKontenplanID,@FiBuBuchungMWSTID,@FiBuBuchungBelegnummer,@FiBuBu
chungBuchungstext,"+
"@FiBuBuchungBetragSoll,@FiBuBuchungBetragHaben, @FiBuBuchungName,
@FiBuBuchungMandantID,@FiBuBuchungUserID)";
cmdInsert.Parameters.Add("@FiBuBuchungID", SqlDbType.BigInt, 8,
"FiBuBuchungID");
cmdInsert.Parameters.Add("@FiBuBuchungBuchungsnummer", SqlDbType.Int, 4,
"FiBuBuchungBuchungsnummer");
cmdInsert.Parameters.Add("@FiBuBuchungBuchungsdatum",
SqlDbType.SmallDateTime, 4, "FiBuBuchungBuchungsdatum");
cmdInsert.Parameters.Add("@FiBuBuchungBuchungstypID", SqlDbType.Int, 4,
"FiBuBuchungBuchungstypID");
cmdInsert.Parameters.Add("@FiBuBuchungKontenplanID", SqlDbType.Int, 4,
"FiBuBuchungKontenplanID");
cmdInsert.Parameters.Add("@FiBuBuchungMWSTID", SqlDbType.Int, 4,
"FiBuBuchungMWSTID");
cmdInsert.Parameters.Add("@FiBuBuchungBelegnummer", SqlDbType.NVarChar,
12, "FiBuBuchungBelegnummer");
cmdInsert.Parameters.Add("@FiBuBuchungBuchungstext", SqlDbType.NVarChar,
50, "FiBuBuchungBuchungstext");
cmdInsert.Parameters.Add("@FiBuBuchungBetragSoll", SqlDbType.Float, 8,
"FiBuBuchungBetragSoll");
cmdInsert.Parameters.Add("@FiBuBuchungBetragHaben", SqlDbType.Float, 8,
"FiBuBuchungBetragHaben");
cmdInsert.Parameters.Add("@FiBuBuchungName", SqlDbType.NVarChar, 30,
"FiBuBuchungName");
cmdInsert.Parameters.Add("@FiBuBuchungMandantID", SqlDbType.Int, 4,
"FiBuBuchungMandantID");
cmdInsert.Parameters.Add("@FiBuBuchungUserID", SqlDbType.Int, 4,
"FiBuBuchungUserID");

cmdInsert.Parameters["@FiBuBuchungID"].SourceVersion =
DataRowVersion.Original;

da.InsertCommand = cmdInsert;
try
{
da2.Update(ds,"tblFiBuBuchung");
}
catch (SqlException sqlExcp)
{
//Bei fehlerhaftem Zugriff wird Fehlermeldung ausgelöst
MessageBox.Show(this, sqlExcp.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
 
Hm,

From where does he know that he must insert everything?

possibly I still changed the data befor in datagrid, of course i save
this data before I would like to write the data into the other table.

I tried the above example. Unfortunately does not go. I always get an
error message with the update

Thanks

Michael
 
Michael said:
Hello NG

i use Microsoft SQL server
and my problem is how i can select table1 into the dataset and insert this
datas into table2 (backup table).

Has someone a idea or a code example?

insert into table2 select * from table1

Make sure that table1 and table2 have the same structure.
 
Back
Top