c#数据库连接、操作、封装dll

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if (cmdtype == 0)
cmd.CommandType = CommandType.Text;
else if (cmdtype == 1)
{
cmd.CommandType = CommandType.StoredProcedure;
spt = new SqlParameter();
for (int i = 0; i <list.Count; i++)
int parbiaoji = -1;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = SQL_SLT;
spt.SqlDbType = SqlDbType.BigInt;
break;
case "Binary":
spt.SqlDbType = SqlDbType.Binary;
break;
case "Bit":
spt.SqlDbType = SqlDbType.Bit;
break;
case "Char;":
spt.SqlDbType = SqlDbType.DateTime2;
break;
case "DateTimeOffset;":
spt.SqlDbType = SqlDbType.DateTimeOffset;
break;
case "Decimal;":
spt.SqlDbType = SqlDbType.Decimal;
if (sqlst == 0)
{
EXSTR=cmd.ExecuteNonQuery().ToString();
sdt.SelectCommand = cmd;
ds = new DataSet();
sdt.Fill(ds);
if (dtbiaoji > 0)
{
OUTPUTSTR = cmd.Parameters[parbiaoji].Value.ToString();
case "Money":
spt.SqlDbType = SqlDbType.Money;
break;
case "NChar":
spt.SqlDbType = SqlDbType.NChar;
break;
case "NText":
{
sqlspa sqlspt = new sqlspa();
sqlspt.name = p1;
sqlspt.spt_type = p2;
sqlspt.spt_value = p3;
sqlspt.spt_length = p4;
sqlspt.dir_val = p5;
list.Add(sqlspt);
}
else if (((来自百度文库qlspa)list[i]).dir_val == 2)
{
spt.Direction = ParameterDirection.Output;
dtbiaoji = 2;
parbiaoji = i;
}
else
{
spt.Direction = ParameterDirection.ReturnValue;
{
OUTPUTSTR = cmd.Parameters[parbiaoji].Value.ToString();
}
return Convert.ToInt32(EXSTR);
}
else
{
sdr = cmd.ExecuteReader();
sdt.SelectCommand = cmd;
ds = new DataSet();
{
name = p1;
spt_type = p2;
spt_value = p3;
spt_length = p4;
dir_val = p5;
}
}
private ArrayList list = new ArrayList(0);
private int n = 0;
public void sqlspaway(string p1,string p2,string p3,int p4,int p5)
break;
case "Float":
spt.SqlDbType = SqlDbType.Float;
break;
case "Image":
spt.SqlDbType = SqlDbType.Image;
break;
case "Int":
spt.SqlDbType = SqlDbType.Int;
break;
spt.Direction = ParameterDirection.Input;
else if (((sqlspa)list[i]).dir_val == 1)
{
spt.Direction = ParameterDirection.InputOutput;
dtbiaoji = 1;
parbiaoji = i;
{
public string name;
public string spt_type;
public string spt_value;
public int spt_length;
public int dir_val;
public void sqlsp(string p1,string p2,string p3,int p4,int p5)
c#数据库连接去sql server
为了简化数据库连接和操作,编写一个数据库dll文件也是比较有用的。只需要设置参数就可以完成数据库操作
属性
OUTPUTSTR存储过程输出
CONSTR数据库连接字符串
SQL_SLT存储过程名或sql语句列表名
代码错误信息返回或连接关闭状态
public int sql_select(int cmdtype, int sqlst,int n)数据库语句执行方法,
1为ParameterDirection.InputOutput、2为ParameterDirection.Output、
大于3为ParameterDirection.ReturnValue
public DataSet ds返回查询结果集
using System;
using System.Collections.Generic;
exstr = value;
}
}
public string SQL_SLT
{
get
{
return sql_slt;
}
set
{
sql_slt = value;
}
}
SqlConnection conn;
public SqlDataReader sdr;
public DataSet ds;
public SqlParameter spt;
}
return Convert.ToInt32(EXSTR);
}
else if (sqlst == 1)
{
EXSTR=cmd.ExecuteScalar().ToString();
sdt.SelectCommand = cmd;
ds = new DataSet();
sdt.Fill(ds);
if (dtbiaoji > 0)
影响的行数
public Boolean Connopen()打开数据库连接方法,返回bool值,true为打开,false
为未打开,并将未打开错误返回到EXSTR属性里
public void conn_close()连接关闭方法,将关闭状态返回到EXSTR属性
public void sqlsp(string p1,string p2,string p3,int p4,int p5)获取SqlParameter
public Boolean Connopen()
{
conn = new SqlConnection(CONSTR);
try
{
conn.Open();
if (conn.State == ConnectionState.Open)
{
return true;
}
}
catch(Exception ex)
{
EXSTR = Convert.ToString(ex);
n = list.Count;
}
public int sql_select(int cmdtype, int sqlst)
{
SqlDataAdapter sdt = new SqlDataAdapter();
SqlParameter[] spts=new SqlParameter[n];
int dtbiaoji = 0;
spt.SqlDbType = SqlDbType.Char;
break;
case "Date;":
spt.SqlDbType = SqlDbType.Date;
break;
case "DateTime;":
spt.SqlDbType = SqlDbType.DateTime;
break;
case "DateTime2":
dtbiaoji = 3;
parbiaoji = i;
}
spts[i] = (spt);
spts[i].Value = ((sqlspa)list[i]).spt_value;
}
cmd.Parameters.AddRange(spts);
}
else
cmd.CommandType = CommandType.TableDirect;
return false;
}
return false;
}
public void conn_close()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
EXSTR = "关闭成功";
}
else
EXSTR = "已关闭";
}
public struct sqlspa
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
namespace connet
对象参数,p1获取SqlParameter.ParameterName属性值,p2获取SqlParameter.sqldbtype属性值,
p3获取SqlParameter.value属性值,p4获取SqlParameter.size属性值,
p5设置SqlParameter.Directions属性值,0为ParameterDirection.Input、
cmdtype设置数据源执行的SQL语句或存储过程,即commandtype属性(0为text枚举,
1为storedprocedure枚举,其他值为tabledirect枚举);
sqlst设置执行SQL语句的方式,0为executenonquery方法执行,1为executereader
方法执行,其他为executescalar方法执行。设置存储过程参数个数。方法返回受
sdt.Fill(ds);
if (dtbiaoji > 0)
{
OUTPUTSTR = cmd.Parameters[parbiaoji].Value.ToString();
}
return 0;
}
}
public void spt_sdtype(string str)
{
switch(str)
{
case "BigInt":
{
public class jdbccoonn
{
private string constr, exstr, sql_slt,outputstr;
private static int q;
public static int Q
{
get
{
return q;
}
set
{
q = value;
}
}
public string OUTPUTSTR
{
spt = new SqlParameter();
spt_sdtype(((sqlspa)list[i]).spt_type);
spt.Size = ((sqlspa)list[i]).spt_length;
spt.ParameterName = ((sqlspa)list[i]).name;
if (((sqlspa)list[i]).dir_val == 0)
{
get
{
return outputstr;
}
set
{
outputstr = value;
}
}
public string CONSTR
{
get
{
return constr;
}
set
{
constr = value;
}
}
public string EXSTR
{
get
{
return exstr;
}
set
{
相关文档
最新文档