CSDN博客

img myschevon

.net中连接和操作数据库的类

发表于2004/9/22 13:58:00  755人阅读

因为个人在学习和使用.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())
{
...
}

0 0

相关博文

我的热门文章

img
取 消
img