datatable 导入mysql 解决_将DataTable中的数据导入到数据库中
上次在
小小的改動即可。
///
/// 將DataTable中數據寫入數據庫中
///
///
///
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 cmdmode = 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(cmdmode, colValues.TrimEnd(','));
}
int ret = 0;
try
{
ret = DbHelperSQL.ExecuteSql(cmd);
}
catch (Exception e)
{
//寫錯誤日志...
string strOuput = string.Format("向數據庫中寫數據失敗,錯誤信息:{0},異常{1}\n", e.Message, e.InnerException);
//將信息寫入到日志輸出文件
DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
}
if (ret == -1)
{
return false;
}
return true;
}
///
/// 寫入基礎數據,并刪除其中的重復的項目
///
///
/// 主鍵
/// 主鍵所在的列
///
public static bool WriteDataToDB(DataTable dt, string KeyName, int icol)
{
//刪除數據庫中的重復項目
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);
//向數據庫中寫入新的數據
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 cmdmode = 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(cmdmode, colValues);
int ret = 0;
try
{
ret = DbHelperSQL.ExecuteSql(cmd);
}
catch (Exception e)
{
//寫錯誤日志...
string strOuput = string.Format("向數據庫中寫數據失敗,錯誤信息:{0},異常{1}\n", e.Message, e.InnerException);
//將信息寫入到日志輸出文件
DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
}
if (ret == -1)
{
return false;
}
}
return true;
}
總結
以上是生活随笔為你收集整理的datatable 导入mysql 解决_将DataTable中的数据导入到数据库中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 基本配置_MySQL 基本配
- 下一篇: linux cmake编译源码,linu