请问高手:求:数据库连接类的代码 我是C#新手,现需要数据库连接类的代码谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.ComponentModel;using System.Collections;using System.Diagnostics;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace Components{ public class DataBase:IDisposable { private SqlConnection con; /// <summary> /// Run stored procedure. /// </summary> /// <param name="procName">存储过程的名字.</param> /// <returns>执行存储过程并返回值.</returns> public int RunProc(string procName) { SqlCommand cmd = CreateCommand(procName, null); cmd.ExecuteNonQuery(); this.Close(); return (int)cmd.Parameters["ReturnValue"].Value; } /// <summary> /// Run stored procedure. /// </summary> /// <param name="procName">存储过程的名字.</param> /// <param name="prams">存储过程 params.</param> /// <returns>执行存储过程并返回值.</returns> public int RunProc(string procName, SqlParameter[] prams) { SqlCommand cmd = CreateCommand(procName, prams); cmd.ExecuteNonQuery(); this.Close(); return (int)cmd.Parameters["ReturnValue"].Value; } //返回结果 public void RunProc(string procName, out SqlDataReader dataReader) { SqlCommand cmd = CreateCommand(procName, null); dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } //执行存储过程,并返回值 public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader) { SqlCommand cmd = CreateCommand(procName, prams); dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } //定义一个指令来调用存储过程 private SqlCommand CreateCommand(string procName, SqlParameter[] prams) { // 打开数据源 Open(); //command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) ); SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.StoredProcedure; // add proc parameters if (prams != null) { foreach (SqlParameter parameter in prams) cmd.Parameters.Add(parameter); } // return param cmd.Parameters.Add( new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return cmd; } /// <summary> /// Open the connection. /// </summary> public void Open() { // open connection if (con == null) { try { con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]); con.Open(); } catch (Exception e) { e.Message.ToString(); } } } /// <summary> /// Close the connection. /// </summary> public void Close() { if (con != null) con.Close(); } /// <summary> /// Release resources. /// </summary> public void Dispose() { // make sure connection is closed if (con != null) { con.Dispose(); con = null; } } /// 输入 param. public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) { return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value); } /// <summary> /// Make input param. /// <returns>New parameter.</returns> public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size) { return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null); } /// Make stored procedure param. public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) { SqlParameter param; if (Size > 0) param = new SqlParameter(ParamName, DbType, Size); else param = new SqlParameter(ParamName, DbType); param.Direction = Direction; if (!(Direction == ParameterDirection.Output && Value == null)) param.Value = Value; return param; } }} using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace database1{ public partial class LoginForm : Form { SqlConnection scn = new SqlConnection(); public LoginForm() { InitializeComponent(); scn = new SqlConnection(); scn.ConnectionString = "Data Source=WANGGANG;Initial Catalog=student;Integrated Security=True"; } private void Login_Load(object sender, EventArgs e) { this.label2.Visible = false; } private void button1_Click(object sender, EventArgs e) { this.label2.Visible = true; try { DataSet ds = new DataSet(); scn.Open(); String strName = this.textName.Text; String strsql = "select Card.密码 from Card join stuInfor on stuInfor.学号=Card.学号 where stuInfor.姓名='" + strName + "'"; SqlDataAdapter da = new SqlDataAdapter(strsql, scn); da.Fill(ds,"stuInfor"); scn.Close(); String tempStr =(String)ds.Tables["stuInfor"].Rows[0]["密码"]; if (tempStr == this.textPassword.Text) { this.label2.Visible = false; MessageBox.Show("恭喜!登陆成功"); StuName = strName; LoginedForm TempForm = new LoginedForm(); TempForm.Show(); this.Hide(); } else MessageBox.Show("密码不正确"); } catch (Exception exception) { MessageBox.Show("不存在此用户名"); } } private void button2_Click(object sender, EventArgs e) { Application.Exit(); } }} using System.Collections.Generic;using System.Text;using System;using System.Collections;using System.Collections.Specialized;using System.Runtime.Remoting.Messaging;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace sectest{ /// <summary> /// ADO.NET数据库操作基础类。 /// </summary> public abstract class sqlaceess { //数据库连接字符串 public static string connectionString = sectest.Properties.Settings.Default.st_scoreConnectionString; public sqlaceess() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString"></param> /// <returns></returns> public static int ExecuteSql(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } } } } /// <summary> /// 执行两条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLString1"></param> /// <param name="SQLString2"></param> public static void ExecuteSqlTran(string SQLString1, string SQLString2) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = connection; SqlTransaction tx = connection.BeginTransaction(); cmd.Transaction = tx; try { cmd.CommandText = SQLString1; cmd.ExecuteNonQuery(); cmd.CommandText = SQLString2; cmd.ExecuteNonQuery(); tx.Commit(); } catch (System.Data.SqlClient.SqlException E) { tx.Rollback(); throw new Exception(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。 /// </summary> /// <param name="SQLStringList"></param> public static void ExecuteSqlTran(string SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { string[] split = SQLStringList.Split(new Char[] { ';' }); foreach (string strsql in split) { if (strsql.Trim() != "") { cmd.CommandText = strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); } catch (System.Data.SqlClient.SqlException E) { tx.Rollback(); throw new Exception(E.Message); } } } /// <summary> /// 执行带一个存储过程参数的的SQL语句。 /// </summary> /// <param name="SQLString"></param> /// <param name="content"></param> /// <returns></returns> public static int ExecuteSql(string SQLString, string content) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, connection); System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } 接上面的/// <summary> /// 向数据库里插入图像格式的字段 /// </summary> /// <param name="strSQL"></param> /// <param name="fs"></param> /// <returns></returns> public static int ExecuteSqlInsertImg(string strSQL, byte[] fs) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(strSQL, connection); System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image); myParameter.Value = fs; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(整数)。 /// </summary> /// <param name="strSQL"></param> /// <returns></returns> public static int GetCount(string strSQL) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(strSQL, connection); try { connection.Open(); SqlDataReader result = cmd.ExecuteReader(); int i = 0; while (result.Read()) { i = result.GetInt32(0); } result.Close(); return i; } catch (System.Data.SqlClient.SqlException e) { throw new Exception(e.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString"></param> /// <returns></returns> public static object GetSingle(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(SQLString, 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.SqlClient.SqlException e) { throw new Exception(e.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行查询语句,返回SqlDataReader /// </summary> /// <param name="strSQL"></param> /// <returns></returns> public static SqlDataReader ExecuteReader(string strSQL) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(strSQL, connection); SqlDataReader myReader; try { connection.Open(); myReader = cmd.ExecuteReader(); return myReader; } catch (System.Data.SqlClient.SqlException e) { throw new Exception(e.Message); } finally { cmd.Dispose(); connection.Close(); } } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString"></param> /// <returns></returns> public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } finally { connection.Close(); } return ds; } } #region 存储过程操作 /// <summary> /// 运行存储过程 /// </summary> /// <param name="storedProcName"></param> /// <param name="parameters"></param> /// <returns></returns> public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlDataReader returnReader; connection.Open(); SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.CommandType = CommandType.StoredProcedure; returnReader = command.ExecuteReader(); //Connection.Close(); return returnReader; } } private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(storedProcName, connection); command.CommandType = CommandType.StoredProcedure; foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } return command; } public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet dataSet = new DataSet(); connection.Open(); SqlDataAdapter sqlDA = new SqlDataAdapter(); sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters); sqlDA.Fill(dataSet, tableName); connection.Close(); return dataSet; } } #endregion }} 麻烦高手写个连接Access数据库的类吧 RS485 和 RS232 在串口通讯编程上有什么区别?因为485是一对多,那么主机怎么与指定的设备通讯呢? 在线等,共通化问题 Finalize()是保护级啊,怎么调用? 请求帮忙!! 谢谢!!急!! 为什么txt无法添加 ADSL? 如何将水晶报表的纸张大小设置永久保存至SQL数据库? 菜鸟问题,关于connection的 对战五子棋 DataView绑定技术 C# 请教 在线急用!多谢了! 求在C#中ORACLE数据库备份,导入实现(大家帮帮忙)
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace Components
{
public class DataBase:IDisposable
{
private SqlConnection con; /// <summary>
/// Run stored procedure.
/// </summary>
/// <param name="procName">存储过程的名字.</param>
/// <returns>执行存储过程并返回值.</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
} /// <summary>
/// Run stored procedure.
/// </summary>
/// <param name="procName">存储过程的名字.</param>
/// <param name="prams">存储过程 params.</param>
/// <returns>执行存储过程并返回值.</returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
//返回结果
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
} //执行存储过程,并返回值
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
//定义一个指令来调用存储过程 private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 打开数据源
Open(); //command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure; // add proc parameters
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
} // return param
cmd.Parameters.Add(
new SqlParameter("ReturnValue", SqlDbType.Int, 4,
ParameterDirection.ReturnValue, false, 0, 0,
string.Empty, DataRowVersion.Default, null)); return cmd;
} /// <summary>
/// Open the connection.
/// </summary>
public void Open()
{
// open connection
if (con == null)
{
try
{
con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();
}
catch (Exception e)
{
e.Message.ToString();
}
}
} /// <summary>
/// Close the connection.
/// </summary>
public void Close()
{
if (con != null)
con.Close();
} /// <summary>
/// Release resources.
/// </summary>
public void Dispose()
{
// make sure connection is closed
if (con != null)
{
con.Dispose();
con = null;
}
} /// 输入 param. public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
} /// <summary>
/// Make input param. /// <returns>New parameter.</returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// Make stored procedure param. public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param; if (Size > 0)
param = new SqlParameter(ParamName, DbType, Size);
else
param = new SqlParameter(ParamName, DbType); param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
param.Value = Value; return param;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace database1
{
public partial class LoginForm : Form
{
SqlConnection scn = new SqlConnection();
public LoginForm()
{
InitializeComponent();
scn = new SqlConnection();
scn.ConnectionString = "Data Source=WANGGANG;Initial Catalog=student;Integrated Security=True";
} private void Login_Load(object sender, EventArgs e)
{
this.label2.Visible = false;
} private void button1_Click(object sender, EventArgs e)
{
this.label2.Visible = true;
try
{
DataSet ds = new DataSet();
scn.Open();
String strName = this.textName.Text;
String strsql = "select Card.密码 from Card join stuInfor on stuInfor.学号=Card.学号 where stuInfor.姓名='" + strName + "'";
SqlDataAdapter da = new SqlDataAdapter(strsql, scn);
da.Fill(ds,"stuInfor");
scn.Close();
String tempStr =(String)ds.Tables["stuInfor"].Rows[0]["密码"];
if (tempStr == this.textPassword.Text)
{
this.label2.Visible = false;
MessageBox.Show("恭喜!登陆成功");
StuName = strName;
LoginedForm TempForm = new LoginedForm();
TempForm.Show();
this.Hide();
} else
MessageBox.Show("密码不正确");
}
catch (Exception exception)
{
MessageBox.Show("不存在此用户名");
} }
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Remoting.Messaging;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace sectest
{
/// <summary>
/// ADO.NET数据库操作基础类。
/// </summary>
public abstract class sqlaceess
{
//数据库连接字符串
public static string connectionString = sectest.Properties.Settings.Default.st_scoreConnectionString;
public sqlaceess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString"></param>
/// <returns></returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// 执行两条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLString1"></param>
/// <param name="SQLString2"></param>
public static void ExecuteSqlTran(string SQLString1, string SQLString2)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
SqlTransaction tx = connection.BeginTransaction();
cmd.Transaction = tx;
try
{
cmd.CommandText = SQLString1;
cmd.ExecuteNonQuery();
cmd.CommandText = SQLString2;
cmd.ExecuteNonQuery();
tx.Commit();
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
/// </summary>
/// <param name="SQLStringList"></param>
public static void ExecuteSqlTran(string SQLStringList)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
SqlTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
string[] split = SQLStringList.Split(new Char[] { ';' });
foreach (string strsql in split)
{
if (strsql.Trim() != "")
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (System.Data.SqlClient.SqlException E)
{
tx.Rollback();
throw new Exception(E.Message);
}
}
}
/// <summary>
/// 执行带一个存储过程参数的的SQL语句。
/// </summary>
/// <param name="SQLString"></param>
/// <param name="content"></param>
/// <returns></returns>
public static int ExecuteSql(string SQLString, string content)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(SQLString, connection);
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@content", SqlDbType.NText);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// 向数据库里插入图像格式的字段
/// </summary>
/// <param name="strSQL"></param>
/// <param name="fs"></param>
/// <returns></returns>
public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(strSQL, connection);
System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Image);
myParameter.Value = fs;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
} }
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(整数)。
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public static int GetCount(string strSQL)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(strSQL, connection);
try
{
connection.Open();
SqlDataReader result = cmd.ExecuteReader();
int i = 0;
while (result.Read())
{
i = result.GetInt32(0);
}
result.Close();
return i;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString"></param>
/// <returns></returns>
public static object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(SQLString, 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.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// 执行查询语句,返回SqlDataReader
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(string strSQL)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(strSQL, connection);
SqlDataReader myReader;
try
{
connection.Open();
myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString"></param>
/// <returns></returns>
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
return ds;
} }
#region 存储过程操作 /// <summary>
/// 运行存储过程
/// </summary>
/// <param name="storedProcName"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataReader returnReader;
connection.Open();
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure; returnReader = command.ExecuteReader();
//Connection.Close();
return returnReader;
}
}
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{ SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command; }
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet, tableName);
connection.Close(); return dataSet;
}
} #endregion
}
}