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

上传人:工**** 文档编号:496475263 上传时间:2023-06-27 格式:DOC 页数:9 大小:40.01KB
返回 下载 相关 举报
c#数据库连接、操作、封装dll_第1页
第1页 / 共9页
c#数据库连接、操作、封装dll_第2页
第2页 / 共9页
c#数据库连接、操作、封装dll_第3页
第3页 / 共9页
c#数据库连接、操作、封装dll_第4页
第4页 / 共9页
c#数据库连接、操作、封装dll_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 机械/制造/汽车 > 汽车技术

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号