using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
namespace DA
{
/// <summary>
///内部类:存储过程的返回值记录类
/// </summary>
public class SqlResult
{
public bool Succeed; //存储过程是否执行成功.
public Hashtable OutputValues; // 存储过程output值,放在(HashTable)表OutputValues里.
public DataTable datatable; //存储过程返回的结果集,放在(DataTable)表datatable里.
public string errorMessage; //访问数据库失败
public SqlResult()
{
Succeed = false;
OutputValues = new Hashtable();
datatable=new DataTable();
errorMessage = "";
}
}
/// <summary>
/// =========***调用存储过程基类***=======
/// </summary>
public abstract class SP_Base : IDisposable
{
public SP_Base() : this("","")
{
}
//重载
public SP_Base(string sp_name, string sql_name)
{
this.ProcedureName = sp_name;
this.SQLName = sql_name;
}
//私有成员变量
private string sp_name; //存储过程名
private string sql_name;
private SqlConnection myConnection;//连接字符串
private SqlCommand myCommand;//SqlCommand用于执行存储过程
private SqlParameter myParameter;//存储过程参数
//公共属性
public string ProcedureName//获取和设置存储过程名
{
get
{
return this.sp_name;
}
set
{
this.sp_name = value;
}
}
//公共属性
public string SQLName//获取和设置存储过程名
{
get
{
return this.sql_name;
}
set
{
this.sql_name = value;
}
}
/// <summary>
/// 调用存储过程
/// </summary>
/// <param name="parameters">参数集合</param>
/// <returns></returns>
public SqlResult Call_SP(params object[] parameters)
{
string strconn=ConfigurationSettings.AppSettings["ConnectionString"];
//存储过程的返回值记录类
SqlResult result = new SqlResult();
myConnection = new SqlConnection(strconn);
myCommand = new SqlCommand(this.ProcedureName, myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
try
{
myConnection.Open();
//将参数添加到存储过程的参数集合
GetProcedureParameter(result,parameters);
//填充数据,将结果填充到SqlResult集中
myAdapter.Fill(result.datatable);
//将输出参数的值添加到Result的OutputValues
GetOutputValue(result);
}
catch(Exception e)
{
result.errorMessage = e.Message;
}
finally
{
myAdapter.Dispose();
myCommand.Dispose();
myConnection.Close();
myConnection.Dispose();
}
return result;
}
/// <summary>
/// 将参数添加到存储过程的参数集合
/// </summary>
/// <param name="parameters"></param>
private void GetProcedureParameter(SqlResult result,params object[] parameters)
{
SqlCommand myCommand2 = new SqlCommand();
myCommand2.Connection = this.myConnection;
myCommand2.CommandText = "select * from INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME='" +this.ProcedureName+ "' order by ORDINAL_POSITION";
SqlDataReader reader = null;
try
{
reader = myCommand2.ExecuteReader();
int i = 0;
while(reader.Read())
{
myParameter = new SqlParameter();
myParameter.ParameterName = reader["PARAMETER_NAME"].ToString();
myParameter.Direction = reader["PARAMETER_MODE"].ToString()=="IN"?ParameterDirection.Input:ParameterDirection.Output;
switch(reader["DATA_TYPE"].ToString())
{
case "bit" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToBoolean(parameters[i]);
myParameter.SqlDbType = SqlDbType.Bit;
break;
case "bigint":
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToInt32(parameters[i]);
myParameter.SqlDbType = SqlDbType.BigInt;
break;
case "int" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToInt32(parameters[i].ToString());
myParameter.SqlDbType = SqlDbType.Int;
break;
case "decimal" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (double)parameters[i];
myParameter.SqlDbType = SqlDbType.Decimal;
myParameter.Precision = (byte)reader["NUMERIC_PRECISION"];
myParameter.Scale = (byte)reader["NUMERIC_SCALE"];
break;
case "nvarchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToString(parameters[i]);
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.NVarChar;
break;
case "varchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.VarChar;
break;
case "nchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.NChar;
break;
case "char" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.Char;
break;
case "ntext" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.SqlDbType = SqlDbType.NText;
break;
case "text" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.SqlDbType = SqlDbType.Text;
break;
case "datetime" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToDateTime(parameters[i]);
myParameter.SqlDbType = SqlDbType.DateTime;
break;
case "smalldatetime" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToDateTime(parameters[i]);
myParameter.SqlDbType = SqlDbType.DateTime;
break;
case "image" :
if(myParameter.Direction == ParameterDirection.Input)
{
myParameter.Value=(byte[])parameters[i];
}
myParameter.SqlDbType = SqlDbType.Image;
break;
case "real":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToSingle(parameters[i]);
myParameter.SqlDbType = SqlDbType.Real;
break;
case "varbinary":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(byte[])parameters[i];
myParameter.SqlDbType = SqlDbType.VarBinary;
break;
default :
break;
}
i++;
myCommand.Parameters.Add(myParameter);
}
}
catch(Exception e)
{
result.errorMessage = e.Message;
}
finally
{
if(reader!=null)
{
reader.Close();
}
myCommand2.Dispose();
}
}
/// <summary>
/// 将输出的值添加到Result的OutputValues
/// </summary>
/// <param name="result"></param>
private void GetOutputValue(SqlResult result)
{
if(result.Succeed==false)
{
result.Succeed=true;
}
foreach(SqlParameter parameter in myCommand.Parameters)
{
if(parameter.Direction == ParameterDirection.Output)
{
//Hashtab表是一个键值对
result.OutputValues.Add(parameter.ParameterName, parameter.Value);
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
protected virtual void Dispose(bool disposing)
{
if (! disposing)
return;
if(myConnection != null)
{
myConnection.Dispose();
}
}
//=======end===============
}
}
调用方法:using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
namespace DA
{
/// <summary>
///内部类:存储过程的返回值记录类
/// </summary>
public class SqlResult
{
public bool Succeed; //存储过程是否执行成功.
public Hashtable OutputValues; // 存储过程output值,放在(HashTable)表OutputValues里.
public DataTable datatable; //存储过程返回的结果集,放在(DataTable)表datatable里.
public string errorMessage; //访问数据库失败
public SqlResult()
{
Succeed = false;
OutputValues = new Hashtable();
datatable=new DataTable();
errorMessage = "";
}
}
/// <summary>
/// =========***调用存储过程基类***=======
/// </summary>
public abstract class SP_Base : IDisposable
{
public SP_Base() : this("","")
{
}
//重载
public SP_Base(string sp_name, string sql_name)
{
this.ProcedureName = sp_name;
this.SQLName = sql_name;
}
//私有成员变量
private string sp_name; //存储过程名
private string sql_name;
private SqlConnection myConnection;//连接字符串
private SqlCommand myCommand;//SqlCommand用于执行存储过程
private SqlParameter myParameter;//存储过程参数
//公共属性
public string ProcedureName//获取和设置存储过程名
{
get
{
return this.sp_name;
}
set
{
this.sp_name = value;
}
}
//公共属性
public string SQLName//获取和设置存储过程名
{
get
{
return this.sql_name;
}
set
{
this.sql_name = value;
}
}
/// <summary>
/// 调用存储过程
/// </summary>
/// <param name="parameters">参数集合</param>
/// <returns></returns>
public SqlResult Call_SP(params object[] parameters)
{
string strconn=ConfigurationSettings.AppSettings["ConnectionString"];
//存储过程的返回值记录类
SqlResult result = new SqlResult();
myConnection = new SqlConnection(strconn);
myCommand = new SqlCommand(this.ProcedureName, myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
try
{
myConnection.Open();
//将参数添加到存储过程的参数集合
GetProcedureParameter(result,parameters);
//填充数据,将结果填充到SqlResult集中
myAdapter.Fill(result.datatable);
//将输出参数的值添加到Result的OutputValues
GetOutputValue(result);
}
catch(Exception e)
{
result.errorMessage = e.Message;
}
finally
{
myAdapter.Dispose();
myCommand.Dispose();
myConnection.Close();
myConnection.Dispose();
}
return result;
}
/// <summary>
/// 将参数添加到存储过程的参数集合
/// </summary>
/// <param name="parameters"></param>
private void GetProcedureParameter(SqlResult result,params object[] parameters)
{
SqlCommand myCommand2 = new SqlCommand();
myCommand2.Connection = this.myConnection;
myCommand2.CommandText = "select * from INFORMATION_SCHEMA.PARAMETERS where SPECIFIC_NAME='" +this.ProcedureName+ "' order by ORDINAL_POSITION";
SqlDataReader reader = null;
try
{
reader = myCommand2.ExecuteReader();
int i = 0;
while(reader.Read())
{
myParameter = new SqlParameter();
myParameter.ParameterName = reader["PARAMETER_NAME"].ToString();
myParameter.Direction = reader["PARAMETER_MODE"].ToString()=="IN"?ParameterDirection.Input:ParameterDirection.Output;
switch(reader["DATA_TYPE"].ToString())
{
case "bit" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToBoolean(parameters[i]);
myParameter.SqlDbType = SqlDbType.Bit;
break;
case "bigint":
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToInt32(parameters[i]);
myParameter.SqlDbType = SqlDbType.BigInt;
break;
case "int" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToInt32(parameters[i].ToString());
myParameter.SqlDbType = SqlDbType.Int;
break;
case "decimal" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (double)parameters[i];
myParameter.SqlDbType = SqlDbType.Decimal;
myParameter.Precision = (byte)reader["NUMERIC_PRECISION"];
myParameter.Scale = (byte)reader["NUMERIC_SCALE"];
break;
case "nvarchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToString(parameters[i]);
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.NVarChar;
break;
case "varchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.VarChar;
break;
case "nchar" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.NChar;
break;
case "char" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.Size = Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType = SqlDbType.Char;
break;
case "ntext" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.SqlDbType = SqlDbType.NText;
break;
case "text" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = (string)parameters[i];
myParameter.SqlDbType = SqlDbType.Text;
break;
case "datetime" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToDateTime(parameters[i]);
myParameter.SqlDbType = SqlDbType.DateTime;
break;
case "smalldatetime" :
if(myParameter.Direction == ParameterDirection.Input)
myParameter.Value = Convert.ToDateTime(parameters[i]);
myParameter.SqlDbType = SqlDbType.DateTime;
break;
case "image" :
if(myParameter.Direction == ParameterDirection.Input)
{
myParameter.Value=(byte[])parameters[i];
}
myParameter.SqlDbType = SqlDbType.Image;
break;
case "real":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToSingle(parameters[i]);
myParameter.SqlDbType = SqlDbType.Real;
break;
case "varbinary":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(byte[])parameters[i];
myParameter.SqlDbType = SqlDbType.VarBinary;
break;
default :
break;
}
i++;
myCommand.Parameters.Add(myParameter);
}
}
catch(Exception e)
{
result.errorMessage = e.Message;
}
finally
{
if(reader!=null)
{
reader.Close();
}
myCommand2.Dispose();
}
}
/// <summary>
/// 将输出的值添加到Result的OutputValues
/// </summary>
/// <param name="result"></param>
private void GetOutputValue(SqlResult result)
{
if(result.Succeed==false)
{
result.Succeed=true;
}
foreach(SqlParameter parameter in myCommand.Parameters)
{
if(parameter.Direction == ParameterDirection.Output)
{
//Hashtab表是一个键值对
result.OutputValues.Add(parameter.ParameterName, parameter.Value);
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
protected virtual void Dispose(bool disposing)
{
if (! disposing)
return;
if(myConnection != null)
{
myConnection.Dispose();
}
}
//=======end===============
}
}
public class bbs_Opera : SP_Base
{
public bbs_Opera()
{
}
//添加新的主题
public bool AddPostInfo(int forumID,int UserID,int postPic,string postTitle,string postText,string postIp)
{
base.ProcedureName = "bbs_AddPostInfo";
SqlResult result = base.Call_SP(UserID,forumID,postPic,postTitle,postText,postIp);
if(!result.Succeed)
{
if(result.errorMessage!="数据源为空")
{
this.errInfo = result.errorMessage;
return false;
}
}
return true;
}
}
}