《c#数据库连接、操作、封装dll》由会员分享,可在线阅读,更多相关《c#数据库连接、操作、封装dll(9页珍藏版)》请在金锄头文库上搜索。
1、c#数据库连接去sql server为了简化数据库连接和操作,编写一个数据库dll文件也是比较有用的。只需要设置参数就可以完成数据库操作属性OUTPUTSTR 存储过程输出CONSTR 数据库连接字符串SQL_SLT 存储过程名或sql语句列表名 代码错误信息返回或连接关闭状态 public int sql_select(int cmdtype, int sqlst,int n) 数据库语句执行方法, cmdtype设置数据源执行的SQL语句或存储过程,即commandtype属性(0为text枚举, 1为storedprocedure枚举,其他值为tabledirect枚举); sqlst设
2、置执行SQL语句的方式,0为executenonquery方法执行,1为executereader 方法执行,其他为executescalar方法执行。设置存储过程参数个数。方法返回受 影响的行数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) 获取SqlParame
3、ter对象参数,p1获取SqlParameter.ParameterName属性值,p2获取SqlParameter.sqldbtype属性值,p3获取SqlParameter.value属性值,p4获取SqlParameter.size属性值,p5设置SqlParameter.Directions属性值,0为ParameterDirection.Input、1为ParameterDirection.InputOutput、2为 ParameterDirection.Output、大于3为ParameterDirection.ReturnValuepublic DataSet ds 返回查询结
4、果集using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;using System.Data;using System.Collections;namespace connet public class jdbccoonn private string constr, exstr, sql_slt,outputstr; private static int q; publi
5、c static int Q get return q; set q = value; public string OUTPUTSTR get return outputstr; set outputstr = value; public string CONSTR get return constr; set constr = value; public string EXSTR get return exstr; set exstr = value; public string SQL_SLT get return sql_slt; set sql_slt = value; SqlConn
6、ection conn; public SqlDataReader sdr; public DataSet ds; public SqlParameter spt; public Boolean Connopen() conn = new SqlConnection(CONSTR); try conn.Open(); if (conn.State = ConnectionState.Open) return true; catch(Exception ex) EXSTR = Convert.ToString(ex); return false; return false; public voi
7、d conn_close() if (conn.State = ConnectionState.Open) conn.Close(); EXSTR = 关闭成功; else EXSTR = 已关闭; public struct sqlspa 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) name
8、 = 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) sqlspa sqlspt = new sqlspa(); sqlspt.name = p1; sqlspt.spt_type = p2; sqlspt.spt_value = p3; sqlspt.sp
9、t_length = p4; sqlspt.dir_val = p5; list.Add(sqlspt); n = list.Count; public int sql_select(int cmdtype, int sqlst) SqlDataAdapter sdt = new SqlDataAdapter(); SqlParameter spts=new SqlParametern; int dtbiaoji = 0; int parbiaoji = -1; if (conn.State = ConnectionState.Closed) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = SQL_SLT; 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+) spt = new SqlParameter(); sp