这篇文章介绍了C#中执行SQL的几种方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
1.不同的数据库
private InfoLightDBTools dbTools; //#ORACLE#
this.dbTools = new InfoLightDBTools(clientInfo, dbName);
private InfoLightDBTXTools dbTXTools; //#ORACLE# 要结合事务处理使用:BeginTransaction(),Commit(),Rollback(),EndTransaction()
this.dbTXTools = new InfoLightDBTXTools(clientInfo, dbName);
private InfoLightMSTools mdbtool; //#SQL SERVER#
2.SQL Server中执行方法
public DataTable GetManPowerData(string vLineName, string vShift)
{
DataTable dt = new DataTable();
ExecutionResult exeRes = new ExecutionResult();
List<SqlParameter> mParams;
SqlParameter mLine_NameParam, mDNSParam;
string sql = @"select *
from manpower
where lineid = (select ID
from lines
where line = @LINENAME
AND (SMTCS IS NULL OR SMTCS = 'C'))
and dns = @dns
order by time1_start
";
mParams = new List<SqlParameter>();
mLine_NameParam = new SqlParameter("@LINENAME", SqlDbType.VarChar, 20);
mLine_NameParam.Value = vLineName;
mParams.Add(mLine_NameParam);
mDNSParam = new SqlParameter("@dns", SqlDbType.VarChar, 20);
mDNSParam.Value = 0;
mParams.Add(mDNSParam);
exeRes = this.rMSDBTools.ExecuteQueryDS(sql, mParams);
if (exeRes.Status)
dt = ((DataSet)exeRes.Anything).Tables[0];
return dt;
}
3.Oracle中执行方法
public DataTable getModel(string line_name)
{
DataTable dt = new DataTable();
ExecutionResult exeRes = new ExecutionResult();
DBParameter dbParam = new DBParameter();
string sql = @" SELECT DISTINCT T.MODEL_NAME
FROM sfism4.R_LINE_MODEL_STATUS_T t
WHERE T.LINE_NAME = :line_name
";
dbParam.Clear();
dbParam.Add("line_name", OracleType.VarChar, line_name);
exeRes = this.mdbtools.ExecuteQueryDS(sql, dbParam.GetParameters());
if (exeRes.Status)
dt = ((DataSet)exeRes.Anything).Tables[0];
return dt;
}
4.执行更新操作
public ExecutionResult DoDelete(string sap_plant, string wip_sn, string up_data1)
{
ExecutionResult exeRes = new ExecutionResult();
exeRes.Message = "";
DBParameter dbParam = new DBParameter();
#region sql
string sql = @" DELETE SFISM4.U_UP2INTERFACE_TODOLIST_T A
WHERE A.CUST_NO = :CUST_NO
AND A.TYPE = 'IMS_STOP'
AND A.JOB_STATUS = 'OK'
AND A.WIP_SN = :WIP_SN
AND A.UP_DATA1 = :UP_DATA1
";
#endregion
#region Param
dbParam.Clear();
dbParam.Add("CUST_NO", OracleType.NVarChar, sap_plant);
dbParam.Add("WIP_SN", OracleType.NVarChar, wip_sn);
dbParam.Add("UP_DATA1", OracleType.NVarChar, up_data1);
#endregion
try
{
exeRes = this.mdbtools.ExecuteUpdate(sql, dbParam.GetParameters());
}
catch (Exception ex)
{
exeRes.Message += ex.Message;
exeRes.Status = false;
}
return exeRes;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#中执行SQL的几种方法讲解
基础教程推荐
猜你喜欢
- unity实现动态排行榜 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- C#类和结构详解 2023-05-30
- C# List实现行转列的通用方案 2022-11-02
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- 一个读写csv文件的C#类 2022-11-06
- ZooKeeper的安装及部署教程 2023-01-22
- C# windows语音识别与朗读实例 2023-04-27
- C# 调用WebService的方法 2023-03-09
- C#控制台实现飞行棋小游戏 2023-04-22