Import data from datatable to database

Source: Internet
Author: User

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;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.