mysql中可以用自定义类型_如何创建并使用数据库通用访问类(即自定义类)

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

namespace Db

{

public class Base

{

public Base()

{

}

protected static SqlConnection conn =new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);

protected static SqlCommand cmd = new SqlCommand(strSp,conn);

protected static SqlDataAdapter da = new SqlDataAdapter();

protected static DataSet ds = new DataSet();

protected static DataView dv = new DataView();

protected static SqlDataReader dr;

protected static SqlParameter[] prams;

protected static string strSp;

protected static SqlDataReader drSelectAll(string strSp)

{

cmd.CommandType = CommandType.StoredProcedure;

conn.Open();

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

return dr;

}//返回一个SqlDataReader

protected static DataSet dsSelectAll(string strSp)

{

da.SelectCommand = new SqlCommand(strSp,conn);

da.SelectCommand.CommandType = CommandType.StoredProcedure;

da.Fill(ds);

conn.Open();

try

{

da.SelectCommand.ExecuteNonQuery();

return ds;

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回一个SqlDataSet

protected static DataView dvSelectAll(string strSp)

{

cmd.CommandType = CommandType.StoredProcedure;

da.SelectCommand = new SqlCommand(strSp,conn);

da.Fill(ds);

conn.Open();

try

{

da.SelectCommand.ExecuteNonQuery();

dv = ds.Tables[0].DefaultView;

return dv;

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回一个DataView

protected static string strCmd(string strSp,SqlParameter[] prams,SqlDataReader dr)

{

CreateCmd(strSp,prams,dr);

conn.Open();

try

{

cmd.ExecuteNonQuery();

return "1";

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回一个数据库操作

protected static SqlCommand CreateCmd(string strSp, SqlParameter[] prams,SqlDataReader dr)

{

cmd.CommandType = CommandType.StoredProcedure;

if (prams != null)

{

foreach (SqlParameter parameter in prams)

cmd.Parameters.Add(parameter);

}

cmd.Parameters.Add(

new SqlParameter("ReturnValue", SqlDbType.Int, 4,

ParameterDirection.ReturnValue, false, 0, 0,

string.Empty, DataRowVersion.Default, null));

return cmd;

}//返回带参数的命令

protected static bool ExecuteSQLs()

{

cmd.CommandType = CommandType.StoredProcedure;

conn.Open();

try

{

int i = (int)cmd.ExecuteScalar();

if(i>0)

{

return true;

}

else

{

return false;

}

}

catch(Exception ex)

{

throw new Exception(ex.Message);

}

finally

{

conn.Dispose();

conn.Close();

}

}//返回第一行的数据操作

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值