The last time we extracted the table data to dataset Based on the path of the Excel file, we introduced how to read the data in the Excel file to dataset, today, I will introduce a method that I used in my project to import data from a able to a database and to write data from a datatable to a database and filter out duplicate data. The code can be used as a demo. If you need an application, you may need
Make a small change.
/// <Summary>
/// Write data in the datatable into the database
/// </Summary>
/// <Param name = "DT"> </param>
/// <Returns> </returns>
Public static bool writedatatodb (datatable DT)
{
If (Dt = NULL | DT. Rows. Count = 0)
{
Return true;
}
String tname = DT. tablename;
String colnames = "";
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Colnames + = DT. Columns [I]. columnname + ",";
}
Colnames = colnames. trimend (',');
String cmd = "";
String colvalues;
String recover mode = string. Format ("insert into {0} ({1}) values ({0});", tname, colnames );
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Colvalues = "";
For (Int J = 0; j <DT. Columns. Count; j ++)
{
If (Dt. Rows [I] [J]. GetType () = typeof (dbnull ))
{
Colvalues + = "null ,";
Continue;
}
If (Dt. Columns [J]. datatype = typeof (string ))
Colvalues + = string. Format ("'{0}',", DT. Rows [I] [J]);
Else if (DT. columns [J]. datatype = typeof (INT) | DT. columns [J]. datatype = typeof (float) | DT. columns [J]. datatype = typeof (double ))
{
Colvalues + = string. Format ("{0},", DT. Rows [I] [J]);
}
Else if (Dt. Columns [J]. datatype = typeof (datetime ))
{
Colvalues + = string. Format ("cast ('{0}' As datetime),", DT. Rows [I] [J]);
}
Else if (Dt. Columns [J]. datatype = typeof (bool ))
{
Colvalues + = string. Format ("{0},", DT. Rows [I] [J]. tostring ());
}
Else
Colvalues + = string. Format ("'{0}',", DT. Rows [I] [J]);
}
Cmd = string. Format (partition mode, colvalues. trimend (','));
}
Int ret = 0;
Try
{
Ret = dbhelpersql. executesql (CMD );
}
Catch (exception E)
{
// Write the error log...
String strouput = string. Format ("failed to write data to database, error message: {0}, exception {1} \ n", E. Message, E. innerexception );
// Write the information to the log output file
Dllcomm. tp_writeapplogfileex (dllcomm. g_applogfilename, strouput );
}
If (ret =-1)
{
Return false;
}
Return true;
}
/// <Summary>
/// Write basic data and delete duplicate projects
/// </Summary>
/// <Param name = "DT"> </param>
/// <Param name = "keyname"> Primary Key </param>
/// <Param name = "icol"> primary key column </param>
/// <Returns> </returns>
Public static bool writedatatodb (datatable DT, string keyname, int icol)
{
// Delete duplicate items in the database
String mkeystr = "";
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Mkeystr + = "'" + dt. Rows [I] [icol] + "',";
}
Mkeystr = mkeystr. Trim (',');
String sqlstr = "delete from" + dt. tablename + "where" + keyname + "in (" + mkeystr + ")";
Dbhelpersql. executesql (sqlstr );
// Write new data to the database
String tname = DT. tablename;
String colnames = "";
For (INT I = 0; I <DT. Columns. Count; I ++)
{
Colnames + = DT. Columns [I]. columnname + ",";
}
Colnames = colnames + "createdate ";
String cmd = "";
String colvalues;
String recover mode = string. Format ("insert into {0} ({1}) values ({0});", tname, colnames );
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Colvalues = "";
For (Int J = 0; j <DT. Columns. Count; j ++)
{
If (Dt. Rows [I] [J]. GetType () = typeof (dbnull ))
{
Colvalues + = "null ,";
Continue;
}
If (Dt. Columns [J]. datatype = typeof (string ))
{
Colvalues + = string. Format ("'{0}',", DT. Rows [I] [J]);
}
Else if (DT. columns [J]. datatype = typeof (INT) | DT. columns [J]. datatype = typeof (float) | DT. columns [J]. datatype = typeof (double ))
{
Colvalues + = string. Format ("{0},", DT. Rows [I] [J]);
}
Else if (Dt. Columns [J]. datatype = typeof (datetime ))
{
Colvalues + = string. Format ("cast ('{0}' As datetime),", DT. Rows [I] [J]);
}
Else if (Dt. Columns [J]. datatype = typeof (bool ))
{
Colvalues + = string. Format ("{0},", DT. Rows [I] [J]. tostring ());
}
Else
Colvalues + = string. Format ("'{0}',", DT. Rows [I] [J]);
}
Colvalues + = "getdate ()";
Cmd = string. Format (partition mode, colvalues );
Int ret = 0;
Try
{
Ret = dbhelpersql. executesql (CMD );
}
Catch (exception E)
{
// Write the error log...
String strouput = string. Format ("failed to write data to database, error message: {0}, exception {1} \ n", E. Message, E. innerexception );
// Write the information to the log output file
Dllcomm. tp_writeapplogfileex (dllcomm. g_applogfilename, strouput );
}
If (ret =-1)
{
Return false;
}
}
Return true;
}