社区
C#
帖子详情
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。这是什么错误啊
alwayslove521
2004-09-14 03:10:39
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。
...全文
187
10
打赏
收藏
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。这是什么错误啊
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。 其他信息: 系统错误。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
10 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
alwayslove521
2004-09-14
打赏
举报
回复
谢谢大家
我再看看
xiaomaoy
2004-09-14
打赏
举报
回复
命名空间有问题,应该这样写:
using System.Data;
using System.Data.SqlClient;
YAOHE
2004-09-14
打赏
举报
回复
try
{
}
catch(System.Exception e)
{
MessageBox.show(e.Message);
}
Jackile
2004-09-14
打赏
举报
回复
头上这么写
using System.Data;
using System.Data.SqlClient;
khpcg
2004-09-14
打赏
举报
回复
不太可能的问题
奇怪?
深山老翁
2004-09-14
打赏
举报
回复
数据库连接不上呀!你看这样可以吗?
“server=服务器名;database=数据库名;uid=sa;password=”
如果有密码记得加上喔!
boyxia
2004-09-14
打赏
举报
回复
数据库连不通,网络问题或连接字串问题。楼上的信誉分怎么这么高啊?从哪里偷来的?
dahuzizyd
2004-09-14
打赏
举报
回复
链接字符串写的有问题吧
alwayslove521
2004-09-14
打赏
举报
回复
string source = "workstation id=GOOD;packet size=4096; "+
" user id=sa;data source=GOOD; "+
" persist security info=False;initial catalog=gwxx ";
SqlConnection coon= new SqlConnection(source);
coon.Open();
//就是在这出现的错误
MessageBox.Show( "123 ");
coon.Close();
alwayslove521
2004-09-14
打赏
举报
回复
string source ="workstation id=GOOD;packet size=4096;"+
" user id=sa;data source=GOOD;"+
" persist security info=False;initial catalog=gwxx";
SqlConnection coon= new SqlConnection(source);
coon.Open();
MessageBox.Show("123");
coon.Close();
已成功与服务器建立连接,但是在登录过程
中
发生
错误
已成功与服务器建立连接,但是在登录过程
中
发生
错误
(provider: 共享内存提供程序, error: 0 - 管道的另一端上无任何进程。) 用户 'sa' 登录失败。该用户与可信
SQL
Server 连接无关联。 说明: 执行当前 Web 请求期间,
出现
未处理
的
异常
。请检查堆栈跟踪信息,以了解有关该
错误
以及代码
中
导致
错误
的出处的详细信息。
异常
详细信息: System.
Data
.
Sql
Client
.
Sql
Exception
: 用户 'sa' 登录失败。该用户与可信
SQL
Server 连接无关联。 说明: 执行当前 Web 请求期间,
出现
未处理
的
异常
。请检查堆栈跟踪信息,以了解有关该
错误
以及代码
中
导致
错误
的出处的详细信息。
异常
详细信息: System.
Data
.
Sql
Client
.
Sql
Exception
: 已成功与服务器建立连接,但是在登录过程
中
发生
错误
。 (provider: 共享内存提供程序, error: 0 - 管道的另一端上无任何进程。) 源
错误
: 行 35: } 行 36: if (con.State == System.
Data
.ConnectionState.Closed) 行 37: con.Open(); 行 38: 行 39: }
Mono.
Data
.
Sql
ite
Mono.
Data
.
Sql
ite
System.Runtime.InteropServices.COMException的解决方法
完美解决“换另一台电脑上用VS2008继续开发web项目时
出现
“System.Runtime.InteropServices.COMException”,然后是加载不了项目。” 只需要打开项目配置文件*.csproj,将True 改为 False,然后可以正常加载项目,接着重新配置为正常的IIS. 您可能感兴趣的文章:System.
Data
.
Sql
Client
.
Sql
Exception
: 无法打开登录所请求的数据库 登录失败。System.
Data
.
SQL
ite 数据库详细介绍
SQL
Server
出现
Syst
CMS.DBUtility.
dll
一款特别好用的.net数据库链接通用类。 using System; using System.Collections; using System.Collections.Specialized; using System.
Data
; using System.
Data
.
Sql
Client
; using System.Configuration; using System.
Data
.Common; using System.Collections.Generic; namespace CMS.DBUtility { ///
/// 数据访问抽象基础类 /// Copyright (C) 2004-2008 By LiTianPing ///
public abstract class DbHelper
SQL
{ //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库. public static string connectionString = PubConstant.ConnectionString; public DbHelper
SQL
() { } #region 公用方法 ///
/// 判断是否存在某表的某个字段 ///
///
表名称 ///
列名称 ///
是否存在
public static bool ColumnExists(string tableName, string columnName) { string
sql
= "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'"; object res = GetSingle(
sql
); if (res == null) { return false; } return Convert.ToInt32(res) > 0; } public static int GetMaxID(string FieldName, string TableName) { string str
sql
= "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(str
sql
); if (obj == null) { return 1; } else { return int.Parse(obj.ToString()); } } public static bool Exists(string str
Sql
) { object obj = GetSingle(str
Sql
); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } } ///
/// 表是否存在 ///
///
///
public static bool TabExists(string TableName) { string str
sql
= "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1"; //string str
sql
= "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')"; object obj = GetSingle(str
sql
); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } } public static bool Exists(string str
Sql
, params
Sql
Parameter[] cmdParms) { object obj = GetSingle(str
Sql
, cmdParms); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = 0; } else { cmdresult = int.Parse(obj.ToString()); } if (cmdresult == 0) { return false; } else { return true; } } #endregion #region 执行简单
SQL
语句 ///
/// 执行
SQL
语句,返回影响的记录数 ///
///
SQL
语句 ///
影响的记录数
public static int Execute
Sql
(string
SQL
String) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) { using (
Sql
Command cmd = new
Sql
Command(
SQL
String, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { connection.Close(); throw e; } } } } public static int Execute
Sql
ByTime(string
SQL
String, int Times) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) { using (
Sql
Command cmd = new
Sql
Command(
SQL
String, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { connection.Close(); throw e; } } } } ///
/// 执行
Sql
和Oracle滴混合事务 ///
///
SQL
命令行列表 ///
Oracle命令行列表 ///
执行结果 0-由于
SQL
造成事务失败 -1 由于Oracle造成事务失败 1-整体事务执行成功
public static int Execute
Sql
Tran(List
list, List
oracleCmd
Sql
List) { using (
Sql
Connection conn = new
Sql
Connection(connectionString)) { conn.Open();
Sql
Command cmd = new
Sql
Command(); cmd.Connection = conn;
Sql
Transaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { foreach (CommandInfo myDE in list) { string cmdText = myDE.CommandText;
Sql
Parameter[] cmdParms = (
Sql
Parameter[])myDE.Parameters; PrepareCommand(cmd, conn, tx, cmdText, cmdParms); if (myDE.EffentNextType == EffentNextType.SolicitationEvent) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { tx.Rollback(); throw new Exception("违背要求"+myDE.CommandText+"必须符合select count(..的格式"); //return 0; } object obj = cmd.ExecuteScalar(); bool isHave = false; if (obj == null && obj == DBNull.Value) { isHave = false; } isHave = Convert.ToInt32(obj) > 0; if (isHave) { //引发事件 myDE.OnSolicitationEvent(); } } if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { tx.Rollback(); throw new Exception("
SQL
:违背要求" + myDE.CommandText + "必须符合select count(..的格式"); //return 0; } object obj = cmd.ExecuteScalar(); bool isHave = false; if (obj == null && obj == DBNull.Value) { isHave = false; } isHave = Convert.ToInt32(obj) > 0; if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { tx.Rollback(); throw new Exception("
SQL
:违背要求" + myDE.CommandText + "返回值必须大于0"); //return 0; } if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { tx.Rollback(); throw new Exception("
SQL
:违背要求" + myDE.CommandText + "返回值必须等于0"); //return 0; } continue; } int val = cmd.ExecuteNonQuery(); if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0) { tx.Rollback(); throw new Exception("
SQL
:违背要求" + myDE.CommandText + "必须有影响行"); //return 0; } cmd.Parameters.Clear(); } string oraConnectionString = PubConstant.GetConnectionString("ConnectionStringPPC"); bool res = OracleHelper.Execute
Sql
Tran(oraConnectionString, oracleCmd
Sql
List); if (!res) { tx.Rollback(); throw new Exception("Oracle执行失败"); // return -1; } tx.Commit(); return 1; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { tx.Rollback(); throw e; } catch (Exception e) { tx.Rollback(); throw e; } } } ///
/// 执行多条
SQL
语句,实现数据库事务。 ///
///
多条
SQL
语句 public static int Execute
Sql
Tran(List
SQL
StringList) { using (
Sql
Connection conn = new
Sql
Connection(connectionString)) { conn.Open();
Sql
Command cmd = new
Sql
Command(); cmd.Connection = conn;
Sql
Transaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { int count = 0; for (int n = 0; n <
SQL
StringList.Count; n++) { string str
sql
=
SQL
StringList[n]; if (str
sql
.Trim().Length > 1) { cmd.CommandText = str
sql
; count += cmd.ExecuteNonQuery(); } } tx.Commit(); return count; } catch { tx.Rollback(); return 0; } } } ///
/// 执行带一个存储过程参数的的
SQL
语句。 ///
///
SQL
语句 ///
参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加 ///
影响的记录数
public static int Execute
Sql
(string
SQL
String, string content) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Sql
Command cmd = new
Sql
Command(
SQL
String, connection); System.
Data
.
Sql
Client
.
Sql
Parameter myParameter = new System.
Data
.
Sql
Client
.
Sql
Parameter("@content",
Sql
DbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { throw e; } finally { cmd.Dispose(); connection.Close(); } } } ///
/// 执行带一个存储过程参数的的
SQL
语句。 ///
///
SQL
语句 ///
参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加 ///
影响的记录数
public static object Execute
Sql
Get(string
SQL
String, string content) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Sql
Command cmd = new
Sql
Command(
SQL
String, connection); System.
Data
.
Sql
Client
.
Sql
Parameter myParameter = new System.
Data
.
Sql
Client
.
Sql
Parameter("@content",
Sql
DbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { throw e; } finally { cmd.Dispose(); connection.Close(); } } } ///
/// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例) ///
///
SQL
语句 ///
图像字节,数据库的字段
类型
为image的情况 ///
影响的记录数
public static int Execute
Sql
InsertImg(string str
SQL
, byte[] fs) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Sql
Command cmd = new
Sql
Command(str
SQL
, connection); System.
Data
.
Sql
Client
.
Sql
Parameter myParameter = new System.
Data
.
Sql
Client
.
Sql
Parameter("@fs",
Sql
DbType.Image); myParameter.Value = fs; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { throw e; } finally { cmd.Dispose(); connection.Close(); } } } ///
/// 执行一条计算查询结果语句,返回查询结果(object)。 ///
///
计算查询结果语句 ///
查询结果(object)
public static object GetSingle(string
SQL
String) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) { using (
Sql
Command cmd = new
Sql
Command(
SQL
String, connection)) { try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { connection.Close(); throw e; } } } } public static object GetSingle(string
SQL
String, int Times) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) { using (
Sql
Command cmd = new
Sql
Command(
SQL
String, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { connection.Close(); throw e; } } } } ///
/// 执行查询语句,返回
Sql
Data
Reader ( 注意:调用该方法后,一定要对
Sql
Data
Reader进行Close ) ///
///
查询语句 ///
Sql
Data
Reader
public static
Sql
Data
Reader ExecuteReader(string str
SQL
) {
Sql
Connection connection = new
Sql
Connection(connectionString);
Sql
Command cmd = new
Sql
Command(str
SQL
, connection); try { connection.Open();
Sql
Data
Reader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return myReader; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { throw e; } } ///
/// 执行查询语句,返回
Data
Set ///
///
查询语句 ///
Data
Set
public static
Data
Set Query(string
SQL
String) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Data
Set ds = new
Data
Set(); try { connection.Open();
Sql
Data
Adapter command = new
Sql
Data
Adapter(
SQL
String, connection); command.Fill(ds, "ds"); } catch (System.
Data
.
Sql
Client
.
Sql
Exception
ex) { throw new Exception(ex.Message); } return ds; } } public static
Data
Set Query(string
SQL
String, int Times) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Data
Set ds = new
Data
Set(); try { connection.Open();
Sql
Data
Adapter command = new
Sql
Data
Adapter(
SQL
String, connection); command.SelectCommand.CommandTimeout = Times; command.Fill(ds, "ds"); } catch (System.
Data
.
Sql
Client
.
Sql
Exception
ex) { throw new Exception(ex.Message); } return ds; } } #endregion #region 执行带参数的
SQL
语句 ///
/// 执行
SQL
语句,返回影响的记录数 ///
///
SQL
语句 ///
影响的记录数
public static int Execute
Sql
(string
SQL
String, params
Sql
Parameter[] cmdParms) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) { using (
Sql
Command cmd = new
Sql
Command()) { try { PrepareCommand(cmd, connection, null,
SQL
String, cmdParms); int rows = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return rows; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { throw e; } } } } ///
/// 执行多条
SQL
语句,实现数据库事务。 ///
///
SQL
语句的哈希表(key为
sql
语句,value是该语句的
Sql
Parameter[]) public static void Execute
Sql
Tran(Hashtable
SQL
StringList) { using (
Sql
Connection conn = new
Sql
Connection(connectionString)) { conn.Open(); using (
Sql
Transaction trans = conn.BeginTransaction()) {
Sql
Command cmd = new
Sql
Command(); try { //循环 foreach (DictionaryEntry myDE in
SQL
StringList) { string cmdText = myDE.Key.ToString();
Sql
Parameter[] cmdParms = (
Sql
Parameter[])myDE.Value; PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } } } } ///
/// 执行多条
SQL
语句,实现数据库事务。 ///
///
SQL
语句的哈希表(key为
sql
语句,value是该语句的
Sql
Parameter[]) public static int Execute
Sql
Tran(System.Collections.Generic.List
cmdList) { using (
Sql
Connection conn = new
Sql
Connection(connectionString)) { conn.Open(); using (
Sql
Transaction trans = conn.BeginTransaction()) {
Sql
Command cmd = new
Sql
Command(); try { int count = 0; //循环 foreach (CommandInfo myDE in cmdList) { string cmdText = myDE.CommandText;
Sql
Parameter[] cmdParms = (
Sql
Parameter[])myDE.Parameters; PrepareCommand(cmd, conn, trans, cmdText, cmdParms); if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine) { if (myDE.CommandText.ToLower().IndexOf("count(") == -1) { trans.Rollback(); return 0; } object obj = cmd.ExecuteScalar(); bool isHave = false; if (obj == null && obj == DBNull.Value) { isHave = false; } isHave = Convert.ToInt32(obj) > 0; if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { trans.Rollback(); return 0; } if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { trans.Rollback(); return 0; } continue; } int val = cmd.ExecuteNonQuery(); count += val; if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0) { trans.Rollback(); return 0; } cmd.Parameters.Clear(); } trans.Commit(); return count; } catch { trans.Rollback(); throw; } } } } ///
/// 执行多条
SQL
语句,实现数据库事务。 ///
///
SQL
语句的哈希表(key为
sql
语句,value是该语句的
Sql
Parameter[]) public static void Execute
Sql
TranWithIndentity(System.Collections.Generic.List
SQL
StringList) { using (
Sql
Connection conn = new
Sql
Connection(connectionString)) { conn.Open(); using (
Sql
Transaction trans = conn.BeginTransaction()) {
Sql
Command cmd = new
Sql
Command(); try { int indentity = 0; //循环 foreach (CommandInfo myDE in
SQL
StringList) { string cmdText = myDE.CommandText;
Sql
Parameter[] cmdParms = (
Sql
Parameter[])myDE.Parameters; foreach (
Sql
Parameter q in cmdParms) { if (q.Direction == ParameterDirection.InputOutput) { q.Value = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); foreach (
Sql
Parameter q in cmdParms) { if (q.Direction == ParameterDirection.Output) { indentity = Convert.ToInt32(q.Value); } } cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } } } } ///
/// 执行多条
SQL
语句,实现数据库事务。 ///
///
SQL
语句的哈希表(key为
sql
语句,value是该语句的
Sql
Parameter[]) public static void Execute
Sql
TranWithIndentity(Hashtable
SQL
StringList) { using (
Sql
Connection conn = new
Sql
Connection(connectionString)) { conn.Open(); using (
Sql
Transaction trans = conn.BeginTransaction()) {
Sql
Command cmd = new
Sql
Command(); try { int indentity = 0; //循环 foreach (DictionaryEntry myDE in
SQL
StringList) { string cmdText = myDE.Key.ToString();
Sql
Parameter[] cmdParms = (
Sql
Parameter[])myDE.Value; foreach (
Sql
Parameter q in cmdParms) { if (q.Direction == ParameterDirection.InputOutput) { q.Value = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); foreach (
Sql
Parameter q in cmdParms) { if (q.Direction == ParameterDirection.Output) { indentity = Convert.ToInt32(q.Value); } } cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } } } } ///
/// 执行一条计算查询结果语句,返回查询结果(object)。 ///
///
计算查询结果语句 ///
查询结果(object)
public static object GetSingle(string
SQL
String, params
Sql
Parameter[] cmdParms) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) { using (
Sql
Command cmd = new
Sql
Command()) { try { PrepareCommand(cmd, connection, null,
SQL
String, cmdParms); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { throw e; } } } } ///
/// 执行查询语句,返回
Sql
Data
Reader ( 注意:调用该方法后,一定要对
Sql
Data
Reader进行Close ) ///
///
查询语句 ///
Sql
Data
Reader
public static
Sql
Data
Reader ExecuteReader(string
SQL
String, params
Sql
Parameter[] cmdParms) {
Sql
Connection connection = new
Sql
Connection(connectionString);
Sql
Command cmd = new
Sql
Command(); try { PrepareCommand(cmd, connection, null,
SQL
String, cmdParms);
Sql
Data
Reader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return myReader; } catch (System.
Data
.
Sql
Client
.
Sql
Exception
e) { throw e; } // finally // { // cmd.Dispose(); // connection.Close(); // } } ///
/// 执行查询语句,返回
Data
Set ///
///
查询语句 ///
Data
Set
public static
Data
Set Query(string
SQL
String, params
Sql
Parameter[] cmdParms) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Sql
Command cmd = new
Sql
Command(); PrepareCommand(cmd, connection, null,
SQL
String, cmdParms); using (
Sql
Data
Adapter da = new
Sql
Data
Adapter(cmd)) {
Data
Set ds = new
Data
Set(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch (System.
Data
.
Sql
Client
.
Sql
Exception
ex) { throw new Exception(ex.Message); } return ds; } } } private static void PrepareCommand(
Sql
Command cmd,
Sql
Connection conn,
Sql
Transaction trans, string cmdText,
Sql
Parameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (cmdParms != null) { foreach (
Sql
Parameter parameter in cmdParms) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } } #endregion #region 存储过程操作 ///
/// 执行存储过程,返回
Sql
Data
Reader ( 注意:调用该方法后,一定要对
Sql
Data
Reader进行Close ) ///
///
存储过程名 ///
存储过程参数 ///
Sql
Data
Reader
public static
Sql
Data
Reader RunProcedure(string storedProcName, I
Data
Parameter[] parameters) {
Sql
Connection connection = new
Sql
Connection(connectionString);
Sql
Data
Reader returnReader; connection.Open();
Sql
Command command = BuildQueryCommand(connection, storedProcName, parameters); command.CommandType = CommandType.StoredProcedure; returnReader = command.ExecuteReader(CommandBehavior.CloseConnection); return returnReader; } ///
/// 执行存储过程 ///
///
存储过程名 ///
存储过程参数 ///
Data
Set结果
中
的表名 ///
Data
Set
public static
Data
Set RunProcedure(string storedProcName, I
Data
Parameter[] parameters, string tableName) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Data
Set
data
Set = new
Data
Set(); connection.Open();
Sql
Data
Adapter
sql
DA = new
Sql
Data
Adapter();
sql
DA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sql
DA.Fill(
data
Set, tableName); connection.Close(); return
data
Set; } } public static
Data
Set RunProcedure(string storedProcName, I
Data
Parameter[] parameters, string tableName, int Times) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) {
Data
Set
data
Set = new
Data
Set(); connection.Open();
Sql
Data
Adapter
sql
DA = new
Sql
Data
Adapter();
sql
DA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sql
DA.SelectCommand.CommandTimeout = Times;
sql
DA.Fill(
data
Set, tableName); connection.Close(); return
data
Set; } } ///
/// 构建
Sql
Command 对象(用来返回一个结果集,而不是一个整数值) ///
///
数据库连接 ///
存储过程名 ///
存储过程参数 ///
Sql
Command
private static
Sql
Command BuildQueryCommand(
Sql
Connection connection, string storedProcName, I
Data
Parameter[] parameters) {
Sql
Command command = new
Sql
Command(storedProcName, connection); command.CommandType = CommandType.StoredProcedure; foreach (
Sql
Parameter parameter in parameters) { if (parameter != null) { // 检查未分配值的输出参数,将其分配以DBNull.Value. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } command.Parameters.Add(parameter); } } return command; } ///
/// 执行存储过程,返回影响的行数 ///
///
存储过程名 ///
存储过程参数 ///
影响的行数 ///
public static int RunProcedure(string storedProcName, I
Data
Parameter[] parameters, out int rowsAffected) { using (
Sql
Connection connection = new
Sql
Connection(connectionString)) { int result; connection.Open();
Sql
Command command = BuildIntCommand(connection, storedProcName, parameters); rowsAffected = command.ExecuteNonQuery(); result = (int)command.Parameters["ReturnValue"].Value; //Connection.Close(); return result; } } ///
/// 创建
Sql
Command 对象实例(用来返回一个整数值) ///
///
存储过程名 ///
存储过程参数 ///
Sql
Command 对象实例
private static
Sql
Command BuildIntCommand(
Sql
Connection connection, string storedProcName, I
Data
Parameter[] parameters) {
Sql
Command command = BuildQueryCommand(connection, storedProcName, parameters); command.Parameters.Add(new
Sql
Parameter("ReturnValue",
Sql
DbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty,
Data
RowVersion.Default, null)); return command; } #endregion } }
Sql
Client
和dbnetlib
Sql
Client
和dbnetlib以及用法,亲测可用!
C#
111,131
社区成员
642,542
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章