日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

将DataTable中的数据导入到数据库中

發(fā)布時(shí)間:2025/7/14 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将DataTable中的数据导入到数据库中 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

????????上次在根據(jù)excel的文件的路徑提取其中表的數(shù)據(jù)到DataSet中 一文中介紹了將Excel文件中的數(shù)據(jù)讀取到DataSet中的方法,今天我來(lái)介紹下我曾經(jīng)在項(xiàng)目中用到的一個(gè)將DataTable中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中的方法和將DataTable中的數(shù)據(jù)寫入數(shù)據(jù)庫(kù)并過(guò)濾掉重復(fù)數(shù)據(jù)的方法。代碼可以給大家作為Demo看下,如果需要應(yīng)用的話,可能需要進(jìn)行

小小的改動(dòng)即可。

/// <summary>
??????? /// 將DataTable中數(shù)據(jù)寫入數(shù)據(jù)庫(kù)中
??????? /// </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 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)
??????????? {
??????????????? //寫錯(cuò)誤日志...
??????????????? string strOuput = string.Format("向數(shù)據(jù)庫(kù)中寫數(shù)據(jù)失敗,錯(cuò)誤信息:{0},異常{1}\n", e.Message, e.InnerException);
??????????????? //將信息寫入到日志輸出文件
??????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
???????????????
??????????? }
??????????? if (ret == -1)
??????????? {
??????????????? return false;
??????????? }
??????????? return true;
???????????
??????? }

??????? /// <summary>
??????? /// 寫入基礎(chǔ)數(shù)據(jù),并刪除其中的重復(fù)的項(xiàng)目
??????? /// </summary>
??????? /// <param name="dt"></param>
??????? /// <param name="KeyName">主鍵</param>
??????? /// <param name="icol">主鍵所在的列</param>
??????? /// <returns></returns>
??????? public static bool WriteDataToDB(DataTable dt, string KeyName, int icol)
??????? {
??????????? //刪除數(shù)據(jù)庫(kù)中的重復(fù)項(xiàng)目
??????????? 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);

??????????? //向數(shù)據(jù)庫(kù)中寫入新的數(shù)據(jù)
??????????? 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)
??????????????? {
??????????????????? //寫錯(cuò)誤日志...
??????????????????? string strOuput = string.Format("向數(shù)據(jù)庫(kù)中寫數(shù)據(jù)失敗,錯(cuò)誤信息:{0},異常{1}\n", e.Message, e.InnerException);
??????????????????? //將信息寫入到日志輸出文件
??????????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
???????????????
??????????????? }
??????????????? if (ret == -1)
??????????????? {
??????????????????? return false;
??????????????? }
??????????? }
??????????? return true;
??????? }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/kevinGao/archive/2011/09/23/2186441.html

總結(jié)

以上是生活随笔為你收集整理的将DataTable中的数据导入到数据库中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。