|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
什么叫数据库怎么样?什么意思?你想单学数据库。(其实变成是我问的)server|server2000|sql|sqlserver|会见|数据usingSystem;
usingSystem.IO;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
usingSystem.Collections;
namespaceSQLServerBase
{
///<summary>
///外部类:存储历程的前往值纪录类
///</summary>
publicclassSqlResult
{
publicboolSucceed;//存储历程是不是实行乐成.
publicHashtableOutputValues;//存储历程output值,放在(HashTable)表OutputValues里.
publicDataTabledatatable;//存储历程前往的了局集,放在(DataTable)表datatable里.
publicDataSetdataSet;//存储历程前往的了局集,放在DataSet表中
publicstringerrorMessage;//会见数据库失利
publicintinflecntNum;
publicSqlResult()
{
Succeed=false;
OutputValues=newHashtable();
datatable=newDataTable();
dataSet=newDataSet();
errorMessage="";
}
}
///<summary>
///====================***挪用存储历程和SQL的基类***============================
///abstract:该类不克不及被实例化,只能经由过程派生子类来利用它
///</summary>
publicabstractclassSpSQL_base:IDisposable
{
publicSpSQL_base():this("","")
{
}
//重载
publicSpSQL_base(stringsp_name,stringsql_name)
{
this.ProcedureName=sp_name;
this.SQLName=sql_name;
}
//公有成员变量
privatestringsp_name;
privatestringsql_name;
privateSqlConnectionmyConnection;
privateSqlCommandmyCommand;
privateSqlParametermyParameter;//存储历程参数
//大众属性
publicstringProcedureName//猎取和设置存储历程名
{
get
{
returnthis.sp_name;
}
set
{
this.sp_name=value;
}
}
//大众属性
publicstringSQLName//猎取和设置存储历程名
{
get
{
returnthis.sql_name;
}
set
{
this.sql_name=value;
}
}
///<summary>
///挪用存储历程
///</summary>
///<paramname="parameters">参数汇合</param>
///<returns></returns>
publicSqlResultCall_SP(paramsobject[]parameters)
{
stringstrconn=ConfigurationSettings.AppSettings["ConnectionString"];
//存储历程的前往值纪录类
SqlResultresult=newSqlResult();
myConnection=newSqlConnection(strconn);
myCommand=newSqlCommand(this.ProcedureName,myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
SqlDataAdaptermyAdapter=newSqlDataAdapter(myCommand);
myConnection.Open();
//将参数增加到存储历程的参数汇合
GetProcedureParameter(result,parameters);
//入手下手事物
using(SqlTransactiontrans=myConnection.BeginTransaction())
{
try
{
if(trans!=null)
{
myCommand.Transaction=trans;
}
//添补数据,将了局添补到SqlResult会合
myAdapter.Fill(result.dataSet);
if(result.dataSet.Tables.Count>0)
result.datatable=result.dataSet.Tables[0].Copy();
//将输入参数的值增加到Result的OutputValues
GetOutputValue(result);
//提交事物
trans.Commit();
}
catch(Exceptione)
{
result.errorMessage=e.Message;
//事物回滚
trans.Rollback();
}
//假如捕获了非常,但仍会实行包含在finally块中的输入语句
finally
{
myAdapter.Dispose();
myCommand.Dispose();
myConnection.Close();
myConnection.Dispose();
}
}
returnresult;
}
///<summary>
///将参数增加到存储历程的参数汇合
///</summary>
///<paramname="parameters"></param>
privatevoidGetProcedureParameter(SqlResultresult,paramsobject[]parameters)
{
SqlCommandmyCommand2=newSqlCommand();
myCommand2.Connection=this.myConnection;
myCommand2.CommandText="select*fromINFORMATION_SCHEMA.PARAMETERSwhereSPECIFIC_NAME="+this.ProcedureName+"orderbyORDINAL_POSITION";
SqlDataReaderreader=null;
try
{
reader=myCommand2.ExecuteReader();
inti=0;
while(reader.Read())
{
myParameter=newSqlParameter();
myParameter.ParameterName=reader["PARAMETER_NAME"].ToString();
myParameter.Direction=reader["PARAMETER_MODE"].ToString()=="IN"?ParameterDirection.Input:ParameterDirection.Output;
switch(reader["DATA_TYPE"].ToString())
{
//bigint
case"bigint":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToInt64(parameters);
myParameter.SqlDbType=SqlDbType.BigInt;
break;
//binary
//bit
case"bit":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToBoolean(parameters);
myParameter.SqlDbType=SqlDbType.Bit;
break;
//char
case"char":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(string)parameters;
myParameter.Size=Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType=SqlDbType.Char;
break;
//datetime
case"datetime":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToDateTime(parameters);
myParameter.SqlDbType=SqlDbType.DateTime;
break;
//decimal
case"decimal":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(decimal)parameters;
myParameter.SqlDbType=SqlDbType.Decimal;
myParameter.Precision=(byte)reader["NUMERIC_PRECISION"];
myParameter.Scale=byte.Parse(reader["NUMERIC_SCALE"].ToString());
break;
//float
case"float":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(float)parameters;
myParameter.SqlDbType=SqlDbType.Float;
break;
//image
case"image":
if(myParameter.Direction==ParameterDirection.Input)
{
myParameter.Value=(byte[])parameters;
}
myParameter.SqlDbType=SqlDbType.Image;
break;
//int
case"int":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToInt32(parameters.ToString());
myParameter.SqlDbType=SqlDbType.Int;
break;
//money
case"money":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToDecimal(parameters);
myParameter.SqlDbType=SqlDbType.Money;
break;
//nchar
case"nchar":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(string)parameters;
myParameter.Size=Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType=SqlDbType.NChar;
break;
//ntext
case"ntext":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(string)parameters;
myParameter.SqlDbType=SqlDbType.NText;
break;
//numeric
case"numeric":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(decimal)parameters;
myParameter.SqlDbType=SqlDbType.Decimal;
myParameter.Precision=(byte)reader["NUMERIC_PRECISION"];
myParameter.Scale=byte.Parse(reader["NUMERIC_SCALE"].ToString());
break;
//nvarchar
case"nvarchar":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToString(parameters);
myParameter.Size=Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType=SqlDbType.NVarChar;
break;
//real
case"real":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToSingle(parameters);
myParameter.SqlDbType=SqlDbType.Real;
break;
//smalldatetime
case"smalldatetime":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToDateTime(parameters);
myParameter.SqlDbType=SqlDbType.DateTime;
break;
//smallint
case"smallint":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToInt16(parameters.ToString());
myParameter.SqlDbType=SqlDbType.SmallInt;
break;
//smallmoney
case"smallmoney":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToDecimal(parameters);
myParameter.SqlDbType=SqlDbType.SmallMoney;
break;
//sql_variant
//text
case"text":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(string)parameters;
myParameter.SqlDbType=SqlDbType.Text;
break;
//timestamp
//tinyint
case"tinyint":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=Convert.ToByte(parameters);
myParameter.SqlDbType=SqlDbType.TinyInt;
break;
//uniqueidentifier
//varbinary
case"varbinary":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(byte[])parameters;
myParameter.SqlDbType=SqlDbType.VarBinary;
break;
//varchar
case"varchar":
if(myParameter.Direction==ParameterDirection.Input)
myParameter.Value=(string)parameters;
myParameter.Size=Convert.ToInt32(reader["CHARACTER_MAXIMUM_LENGTH"]);
myParameter.SqlDbType=SqlDbType.VarChar;
break;
default:
break;
}
i++;
myCommand.Parameters.Add(myParameter);
}
}
catch(Exceptione)
{
result.errorMessage=e.Message;
}
finally
{
if(reader!=null)
{
reader.Close();
}
myCommand2.Dispose();
}
}
///<summary>
///将输入的值增加到Result的OutputValues
///</summary>
///<paramname="result"></param>
privatevoidGetOutputValue(SqlResultresult)
{
if(result.Succeed==false)
{
result.Succeed=true;
}
foreach(SqlParameterparameterinmyCommand.Parameters)
{
if(parameter.Direction==ParameterDirection.Output)
{
//Hashtab表是一个键值对
result.OutputValues.Add(parameter.ParameterName,parameter.Value);
}
}
}
publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
protectedvirtualvoidDispose(booldisposing)
{
if(!disposing)
return;
if(myConnection!=null)
{
myConnection.Dispose();
}
}
//=======end======
//=======begin====
///<summary>
///挪用SQL的基类
///</summary>
///<paramname="parameters">参数汇合</param>
///<returns></returns>
publicSqlResultCall_SQL()
{
stringstrconn=ConfigurationSettings.AppSettings["ConnectionString"];
//存储历程的前往值纪录类
SqlResultresult=newSqlResult();
myConnection=newSqlConnection(strconn);
myCommand=newSqlCommand(this.sql_name,myConnection);
myCommand.CommandType=CommandType.Text;
SqlDataAdaptermyAdapter=newSqlDataAdapter(myCommand);
myConnection.Open();
using(SqlTransactiontrans=myConnection.BeginTransaction())
{
try
{
if(trans!=null)
{
myCommand.Transaction=trans;
}
//添补数据,将了局添补到SqlResult会合
myAdapter.Fill(result.datatable);
result.Succeed=true;
//提交事物
trans.Commit();
}
catch(Exceptione)
{
result.Succeed=false;
result.errorMessage=e.Message;
}
//假如捕获了非常,但仍会实行包含在finally块中的输入语句
finally
{
myAdapter.Dispose();
myCommand.Dispose();
myConnection.Close();
myConnection.Dispose();
}
}
returnresult;
}
//=======end=========
}
}
承继此类后间接挪用,以下:DB:Northwind
publicclassDATest:SpSQL_base
{
publicDATest()
{}
publicSqlResultSQLTest()
{
base.SQLName="selectEmployeeID,LastNamefromdbo.Employees";
returnbase.Call_SQL();
}
publicSqlResultSPTest()
{
base.ProcedureName="CustOrderHist";
returnbase.Call_SP("ALFKI");
}
}设置文件
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<appSettings>
<addkey="ConnectionString"value="server=BIM-7C67612053C;database=Northwind;uid=sa;pwd=;"/>
</appSettings>
</configuration>显现查询了局:
privatevoidDataShow_Load(objectsender,System.EventArgse)
{
DATestda=newDATest();
if(da.SPTest().Succeed&&da.SQLTest().Succeed)
{
this.dataSP.DataSource=da.SPTest().datatable;
this.dataSQL.DataSource=da.SQLTest().datatable;
}
}
此类另有待完美,诸如存储历程参数为//binary、//sql_variant、//timestamp、//uniqueidentifier这些范例时还不克不及实行查询,其他BUG请人人多多斧正~~
感激之前一同在二炮事情过的师兄们供应源代码,小弟只是做复杂修正,感谢列位师兄!
出处:shanvenleoBLOG
在VC.net的版本上,为了让C++运行在.NETFramework中,微软为C++引进了托管,就是托管C++(ManagedC++),这个根本就没有流行起来,自托管C++产生以后就没有收到过好评。 |
|