.net中连接和操作数据库的类
来源:互联网 发布:天津有mac专柜吗 编辑:程序博客网 时间:2024/06/03 03:19
因为个人在学习和使用.NET的过程中,为了方便对数据库的连接和操作,查阅了一些资料后根据自己的需要写成此类。
功能:连接MS SQL数据库
读取INI文件
操作数据库并返回相应结果
INI文件内容:
[dbconnection]
server=localhost
database=mydatabase
db_user=sa
db_pass=
文件名:Mydb.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text.RegularExpressions;
using System.IO;
using System.Web.Mail;
using System.Text;
using System.Xml;
using System.Runtime.InteropServices;
namespace Linkdb
{
public class Mydb
{
public string path; //INI文件名
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
public string ConnStr;
private int Index;
public Mydb()
{
//IniFile(HttpContext.Current.Server.MapPath("/webwork/dbconn.ini"));
IniFile(System.Environment.SystemDirectory+"//dbconn.ini");
string server=ReadValue("dbconnection","server");
string database=ReadValue("dbconnection","database");
string db_user=ReadValue("dbconnection","db_user");
string db_pass=ReadValue("dbconnection","db_pass");
ConnStr="Provider=SQLOLEDB;Data Source="+server+";Initial Catalog="+database+";User ID="+db_user+";Password="+db_pass+"";
}
public bool TestLink()
{
try
{
OleDbConnection Conn;
Conn=new OleDbConnection(ConnStr);
Conn.Open();
return true;
}
catch(Exception)
{
return false;
}
}
public void IniFile(string INIPath)
{
path = INIPath;
}
public void WriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}//写ini
public string ReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
return temp.ToString();
}//读ini
public OleDbDataReader GetDataReader(string SelectSQL)
{
OleDbConnection Conn;
OleDbCommand Cmd;
OleDbDataReader myDataReader;
Conn= new OleDbConnection(ConnStr);
Conn.Open();
Cmd = new OleDbCommand(SelectSQL,Conn);
myDataReader=Cmd.ExecuteReader();
return myDataReader;
}
public DataView GetDataView(string SelectSQL)
{
OleDbConnection Conn;
OleDbDataAdapter Cmd;
DataSet ds;
string TableName;
Conn= new OleDbConnection(ConnStr);
Cmd = new OleDbDataAdapter(SelectSQL, Conn);
Index+=1;
TableName ="Table"+Index;
ds=new DataSet();
Cmd.Fill(ds, TableName);
return(new DataView(ds.Tables[TableName]));
}
public void ExecuteSQL(string ActionQuery)
{
OleDbConnection Conn;
Conn= new OleDbConnection(ConnStr);
Conn.Open();
OleDbCommand Cmd;
Cmd = new OleDbCommand(ActionQuery,Conn);
Cmd.ExecuteNonQuery();
}
}
}
编译为dll文件
控制台中输入:csc /target:library Mydb.cs Mydb.dll
应用此类;
先在工程中引用此类
在页面中引用命名空间:
using Linkdb;
继承此类:
protected Mydb db = new Mydb();
如执行查询:
string sqlstr = “select * from mytable”;
OleDbDataReader dr1 = db.GetDataReader(sqlstr);
if(dr1.Read())
{
...
}
- .net中连接和操作数据库的类
- .net中webform和winform连接sql server 2000数据库的c#操作类
- .net中webform和webform连接sql server 2000数据库的c#操作类
- dot net framework 中进行连接db2数据库的操作
- vb.net access数据库,常用的连接数据库和记录集操作的方法
- ASP.NET中数据库的操作初步----连接并且打开数据库
- ASP.NET中数据库的操作初步----连接并且打开数据库
- ASP.NET中数据库的操作初步----连接并且打开数据库
- ASP.NET(C#)连接数据库和操作数据库
- ASP.NET(C#)连接数据库和操作数据库
- c#.net连接access操作类 (转)|| C#中如何连接Access2007数据库
- 自己写的个C#(ASP.NET)连接SQL数据库执行查询和修改操作的类
- .net中连接sql数据库的字符串
- Asp.net配置文件中数据库的连接
- 注意数据库操作中打开的连接
- c# .net 连接数据库操作
- ASP.NET连接数据库操作
- mysql数据库的连接和操作
- Shell操作与简易编程(一)
- Build Your ASP.NET Pages on a Richer Bedrock
- 耐得住一个人努力的寂寞才能做出一番事业
- 我是天才
- Shell操作与简易编程(二)
- .net中连接和操作数据库的类
- 最近倒霉的事真多!
- Shell操作与简易编程(三)
- Shell操作与简易编程(四)
- VB中類模塊實現與C++中類實現的比較(1)
- 文件上传及下载
- Roller的0.9.9.2的Demo发布了
- SAS9新体验-在DATA STEP中使用perl 正则表达式支持(Regular Expressions)
- Shell操作与简易编程(五)