C#如何将datatable中的数据批量更新到MYSQL数据库
datatable中的数据就是从数据库表中读取的,我现在想修改了datatable一个数据,然后再更新到数据库这个表中,注意不是重新插入,是把原来数据进行更新!!!谢谢...
datatable中的数据就是从数据库表中读取的,我现在想修改了datatable一个数据,然后再更新到数据库这个表中,注意不是重新插入,是把原来数据进行更新!!!谢谢
展开
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;
}
第一步:打开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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询