/// <summary> /// 更新内存中的数据到数据库 /// </summary> /// <param name="tableName"></param> public void UpdateDataSetToDB(string tableName) { HttpContext.Current.Application.Lock(); try { DataSet myDS=(DataSet)HttpContext.Current.Application["aDataSet"]; SqlDataAdapter myDA= (SqlDataAdapter)HttpContext.Current.Application["aDA"]; SqlCommandBuilder sbTemp=new SqlCommandBuilder(myDA); myDA.Update(myDS,tableName); } catch(Exception ex) { WebLog.WriteLog("更新内存中的数据到数据库出现了错误:" + ex.Message); } HttpContext.Current.Application.UnLock(); }
我写的一个更新一个datable的方法,给你参考一下
public string ExecuteInsert(DataTable dt) { string sql=""; string fieldStr=""; string valueStr=""; int i=0; string result; try { for(i=0;i<dt.Columns.Count;i++) { fieldStr += dt.Columns[i].ColumnName + ","; }
fieldStr = fieldStr.Substring(0,fieldStr.Length-1); for(i=0;i<dt.Rows.Count;i++) { sql = "insert into {0}({1}) values({2})"; valueStr =""; for(int j=0;j<dt.Columns.Count;j++) { switch(System.Type.GetTypeCode(dt.Rows[i][j].GetType())) { case System.TypeCode.Byte: case System.TypeCode.Char: case System.TypeCode.Decimal: case System.TypeCode.Double: case System.TypeCode.Int16: case System.TypeCode.Int32: case System.TypeCode.Int64: case System.TypeCode.SByte: case System.TypeCode.Single: case System.TypeCode.UInt16: case System.TypeCode.UInt32: case System.TypeCode.UInt64: valueStr += dt.Rows[i][j].ToString() +","; break; case System.TypeCode.DateTime: case System.TypeCode.String: valueStr += "'" + dt.Rows[i][j].ToString() +"',"; break; case System.TypeCode.Boolean: { if ((bool)dt.Rows[i][j]==true) valueStr += "1,"; else valueStr += "0,";
break; } default: valueStr +=" null,"; break; } } valueStr = valueStr.Substring(0,valueStr.Length-1); sql = string.Format(sql,dt.TableName,fieldStr,valueStr); result = this.ExecuteNonQuery(sql); } return "ok"; }
catch(Exception ex) { return ex.Message;
} }
[文书写作]关于长时间没有更新UChome的话,supesite首页就不… [办公软件]批量删除Office文档(word,excle,powerpoint)中的超… [办公软件]在PowerPoint中插入自动更新的日期和时间(让页眉页… [办公软件]利用幻灯片搜索器高效、快速批量复制另外一个演示… [办公软件]Office文档(Word,PPT)的作者、备注、最后修改时间… [办公软件]Excel自动切换输入法和批量删除空行 [Web开发]用SqlCommandBuilder 实现批量更新 [办公软件]利用Excel宏代码快速批量删除数据列的重复值 [办公软件]在Excel中如何按一定要求使用VBA批量修改数据量非… [办公软件]此工作簿包含其他数据源的链接,是否需要更新?
|