如何将DataTable更新到数据库中

如何将DataTable更新到数据库中,第1张

SqlDataAdapter adapter = new SqlDataAdapter(strSql, strConn)

SqlCommandBuilder builder = new SqlCommandBuilder(adapter)

DataSet ds = new DataSet()

adapter.Fill(ds, "Titles")

//插入记录

DataTable table = ds.Tables["Titles"]

DataRow row = table.NewRow()

row["Title_id"] = "JP1001"

row["title"] = "programming Microsoft .NET"

row["price"] = 59.99m

row["ytd_sales"] = 100000

row["type"] = "business"

row["pubdate"] = new DateTime(2002, 5, 1)

table.Rows.Add(row)

//更新数据库

adapter.Update(table)

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

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存