The
DataTable represents a memory-related datasheet that can be created and used using control drag-and-drop in the toolbar, or created and used independently as needed in the programming process, most commonly as a member of a dataset. In this case, you need to create the data table dynamically as needed during the programming process
public void inserttable (DataTable dt, String tabelname, DataColumnCollection dtcolum)
{
& nbsp;
//declaring database connections
Mycon. Open ();
//declaring SqlBulkCopy, using to release unmanaged Resources
using (sqlbulkcopy SQLBC = new SqlBulkCopy (mycon))
{
& nbsp; //One-time BULK insert data volume
sqlbc.batchsize = 1000;
The number of seconds allowed to complete the operation before the //timeout, if the transaction is not committed and the data is rolled back, All replicated rows will be removed from the target table
sqlbc.bulkcopytimeout = 60 ;
The Notifyafter property to call the corresponding event each time you insert 10,000 data.
Sqlbc.notifyafter = 10000;
Sqlbc.sqlrowscopied + = new Sqlrowscopiedeventhandler (onsqlrowscopied);
Set up tables to be written in bulk
Sqlbc.destinationtablename = Tabelname;
The custom DataTable corresponds to the fields of the database
SQLBC.COLUMNMAPPINGS.ADD ("id", "tel");
SQLBC.COLUMNMAPPINGS.ADD ("name", "Neirong");
for (int i = 0; i < Dtcolum.count; i++)
{
SQLBC.COLUMNMAPPINGS.ADD (Dtcolum[i]. Columnname.tostring (), Dtcolum[i]. Columnname.tostring ());
}
Bulk Write
Sqlbc.writetoserver (DT);
}
Mycon. Dispose ();
}
event when responding
void Onsqlrowscopied (object sender, Sqlrowscopiedeventargs e)
{
Response.Write ("<br/> ok!");
}
Dataadapter.selectcommand is used to obtain data, so its CommandText should be a SELECT statement;
Dataadapter.insertcommand is used to insert data, so its CommandText should be an INSERT statement;
Dataadapter.deletecommand is used to delete data, so its CommandText should be the DELETE statement;
Dataadapter.updatecommand is used to update the data, so its CommandText should be the UPDATE statement;
So your code should write this:
Selectsql= "SELECT * from Tblname";
SqlCommand selectcmd = new SqlCommand (Selectsql, Con);
Insertsql= "INSERT into Tblname (input_time,location_id) VALUES (@Input_Time, @Location_ID)";
SqlCommand insertcmd = new SqlCommand (Insertsql, Con);
INSERTCMD.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@Input_Time", System.Data.SqlDbType.Time));
INSERTCMD.PARAMETERS.ADD (New System.Data.SqlClient.SqlParameter ("@Location_ID", System.Data.SqlDbType.NVarChar)) ;
SqlDataAdapter Dad = new SqlDataAdapter ();
Dad.selectcommand = Selectcmd;
Dad.insertcommand = Insertcmd;
Dad.update (DT);