C#如何将datatable中的数据批量更新到MYSQL数据库

C#如何将datatable中的数据批量更新到MYSQL数据库,第1张

C#利用update更新数据到oracle数据库

第一步:打开oracle数据库

public static string connString = "";

public static OracleConnection conn = null;

//打开数据库连接

public static bool Open()

{

//从配置文件中获取连接字符串

//配置文件需要放在项目目录下的bin\Release中

connString = getXmlValue("connString");

conn = new OracleConnection(getXmlValue("connString"));

try

{

connOpen();

ConsoleWriteLine("数据库连接成功");

return true;

}

catch (SystemException ex)

{

ConsoleWrite(exMessage);

MessageBoxShow("未能连接到数据库");

return false;

}

第二步:执行更新方法:

public static int update(int ruleId, int equipmentId, String equipmentName, String propertyName, int ruleType, String ruleRequest, String ruleRequestOther, String ruleExplain)

{

String sql = "update Device_Attr t set tBASEID = :equipmentId, tBASENAME = :equipmentName,tCA_NAME = :propertyName,tCA_RULETYPE = :ruleType,tCA_RULETEXT = :ruleRequest,tCA_RULETEXT2 = :ruleRequestOther,tCONTENT = :ruleExplain where tCA_ID =:ruleId";

OracleCommand cmd = new OracleCommand(sql, conn);

OracleParameter param_1 = new OracleParameter(":equipmentId", equipmentId);

cmdParametersAdd(param_1);

OracleParameter param_2 = new OracleParameter(":equipmentName", equipmentName + "$");

cmdParametersAdd(param_2);

OracleParameter param_3 = new OracleParameter(":propertyName", propertyName);

cmdParametersAdd(param_3);

OracleParameter param_4 = new OracleParameter(":ruleType", ruleType);

cmdParametersAdd(param_4);

OracleParameter param_5 = new OracleParameter(":ruleRequest", ruleRequest);

cmdParametersAdd(param_5);

OracleParameter param_6 = new OracleParameter(":ruleRequestOther", ruleRequestOther);

cmdParametersAdd(param_6);

OracleParameter param_7 = new OracleParameter(":ruleExplain", ruleExplain);

cmdParametersAdd(param_7);

OracleParameter param_8 = new OracleParameter(":ruleId", ruleId);

cmdParametersAdd(param_8);

int result = cmdExecuteNonQuery();

return result;

}

下面是实现的代码: DataTable table = new DataTable();//初始化一个DataTable对象 string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//连接数据库 SqlCommand sqlCommand = new SqlCommand("select from testB", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//这句挺重要的,好像是批量更新的时候用 //DataSet dtst = new DataSet(); 用dataSet也可以完成,下面有对应实现的代码 sqlConnectionOpen(); sqlAdapFill(table); //table = dtstTables["testB"]; DataRow dr = tableRows[0];//第一行 //drBeginEdit(); //drEndEdit(); 貌似这两句主要是数据绑定到控件的时候才用 for (int i = 0; i < tableRowsCount; i++) //修改table中每一行的内容 { dr = tableRows[i]; dr["f"] = 3; //字段名为f } //testB表中必须存在主键,否则无法更新// 这个很重要 sqlAdapUpdate(table); //sqlAdapUpdate(dtst, "testB"); sqlConnectionClose(); MessageBoxShow("aa"); 下面是DataTable与dataGridView 控件绑定,当dataGridView中的数据改变时,更新数据库的代码: DataTable和dataGridView控件绑定代码:private void Form1_Load(object sender, EventArgs e) { string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//初始化sqlconnection DataTable table = new DataTable(); SqlDataAdapter sqlAdap = new SqlDataAdapter("select from testB ", sqlConnection); sqlConnectionOpen(); sqlAdapFill(table); sqlConnectionClose(); //将testB表中的数据显示在dataGridView中 thisdataGridView1DataSource = table; } 实现将dataGridView中的改动更新到数据库代码:private void butUpdate_Click(object sender, EventArgs e) { DataTable table = new DataTable(); table = (DataTable)thisdataGridView1DataSource; string sqlConnectionString = "Data Source=WangHaitao-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;"; SqlConnection sqlConnection = new SqlConnection(sqlConnectionString); SqlCommand sqlCommand = new SqlCommand("select from testB", sqlConnection); SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//必须有 sqlConnectionOpen(); sqlAdapFill(table); //testB表中必须存在主键,否则无法更新 sqlAdapUpdate(table); sqlConnectionClose(); MessageBoxShow("aa"); }

用update语句啊!通过循环访问第一列的数据 for (int i = 0; i < dsTables[0]RowsCount-1; i++)

{

object[] arry = dsTables[0]Rows[i]ItemArray;//把每一行的数据复制给给arry数组

SqlCommand cmd= new SqlCommand("update tanle1 set NM='"+arry[0]ToString ()+"'",con);

cmdExecuteNonQuery();

}相信你后面就可以搞定了吧祝你成功哈!

以上就是关于C#如何将datatable中的数据批量更新到MYSQL数据库全部的内容,包括:C#如何将datatable中的数据批量更新到MYSQL数据库、DataTable修改后,如何更新数据库、如何把数据集的第一列数据更新到数据库的table1表的第一列等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10154782.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存