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

2025-01-01 05:34:07
推荐回答(1个)
回答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
{
conn.Open();
Console.WriteLine("数据库连接成功");
return true;
}
catch (System.Exception ex)
{
Console.Write(ex.Message);
MessageBox.Show("未能连接到数据库");
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 t.BASEID = :equipmentId, t.BASENAME = :equipmentName,t.CA_NAME = :propertyName,t.CA_RULETYPE = :ruleType,t.CA_RULETEXT = :ruleRequest,t.CA_RULETEXT2 = :ruleRequestOther,t.CONTENT = :ruleExplain where t.CA_ID =:ruleId";
OracleCommand cmd = new OracleCommand(sql, conn);
OracleParameter param_1 = new OracleParameter(":equipmentId", equipmentId);
cmd.Parameters.Add(param_1);
OracleParameter param_2 = new OracleParameter(":equipmentName", equipmentName + "$");
cmd.Parameters.Add(param_2);
OracleParameter param_3 = new OracleParameter(":propertyName", propertyName);
cmd.Parameters.Add(param_3);
OracleParameter param_4 = new OracleParameter(":ruleType", ruleType);
cmd.Parameters.Add(param_4);
OracleParameter param_5 = new OracleParameter(":ruleRequest", ruleRequest);
cmd.Parameters.Add(param_5);
OracleParameter param_6 = new OracleParameter(":ruleRequestOther", ruleRequestOther);
cmd.Parameters.Add(param_6);
OracleParameter param_7 = new OracleParameter(":ruleExplain", ruleExplain);
cmd.Parameters.Add(param_7);
OracleParameter param_8 = new OracleParameter(":ruleId", ruleId);
cmd.Parameters.Add(param_8);
int result = cmd.ExecuteNonQuery();
return result;
}