当前位置:
文档之家› c#数据库连接、操作、封装dll
c#数据库连接、操作、封装dll
}
return Convert.ToInt32(EXSTR);
}
else if (sqlst == 1)
{
EXSTR=cmd.ExecuteScalar().ToString();
sdt.SelectCommand = cmd;
ds = new DataSet();
sdt.Fill(ds);
if (dtbiaoji > 0)
对象参数,p1获取SqlParameter.ParameterName属性值,p2获取SqlParameter.sqldbtype属性值,
p3获取SqlParameter.value属性值,p4获取SqlParameter.size属性值,
p5设置SqlParameter.Directions属性值,0为ParameterDirection.Input、
if (cmdtype == 0)
mandType = CommandType.Text;
else if (cmdtype == 1)
{
mandType = CommandType.StoredProcedure;
spt = new SqlParameter();
for (int i = 0; i <list.Count; i++)
spt.SqlDbType = SqlDbType.DateTime2;
break;
case "DateTimeOffset;":
spt.SqlDbType = SqlDbType.DateTimeOffset;
break;
case "Decimal;":
spt.SqlDbType = SqlDbType.Decimal;
spt.SqlDbType = SqlDbType.Char;
break;
case "Date;":
spt.SqlDbType = SqlDbType.Date;
break;
case "DateTime;":
spt.SqlDbType = SqlDbType.DateTime;
break;
case "DateTime2":
{
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)
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
namespace connet
}
else if (((sqlspa)list[i]).dir_val == 2)
{
spt.Direction = ParameterDirection.Output;
dபைடு நூலகம்biaoji = 2;
parbiaoji = i;
}
else
{
spt.Direction = ParameterDirection.ReturnValue;
{
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)
{
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
影响的行数
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
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;
break;
case "Float":
spt.SqlDbType = SqlDbType.Float;
break;
case "Image":
spt.SqlDbType = SqlDbType.Image;
break;
case "Int":
spt.SqlDbType = SqlDbType.Int;
break;
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 Boolean Connopen()
{
conn = new SqlConnection(CONSTR);
try
{
conn.Open();
if (conn.State == ConnectionState.Open)
{
return true;
}
}
catch(Exception ex)
{
EXSTR = Convert.ToString(ex);
dtbiaoji = 3;
parbiaoji = i;
}
spts[i] = (spt);
spts[i].Value = ((sqlspa)list[i]).spt_value;
}
cmd.Parameters.AddRange(spts);
}
else
mandType = CommandType.TableDirect;
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;
{
get
{
return outputstr;
}
set
{
outputstr = value;
}
}
public string CONSTR
{
get
{
return constr;
}
set
{
constr = value;
}
}
public string EXSTR
{
get
{
return exstr;
}
set
{
{
OUTPUTSTR = cmd.Parameters[parbiaoji].Value.ToString();
}
return Convert.ToInt32(EXSTR);
}
else
{
sdr = cmd.ExecuteReader();
sdt.SelectCommand = cmd;
ds = new DataSet();
int parbiaoji = -1;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand();