以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  请教基于.net的sql server访问基础类库的开发的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=31205)


--  作者:ronnyma
--  发布时间:4/25/2006 9:38:00 AM

--  请教基于.net的sql server访问基础类库的开发的问题
有一段基础类库方面的代码,
帮帮忙看看,解释一下,我看不太懂!!
using System;
using System.Data;

namespace EMCL_DataBase
{
 /// <summary>
 /// Class_DataDase 的摘要说明。
 /// 该类提供用于访问MS SQL SERVER数据库的方法
 /// 该类提供如下的方法:
 ///
 ///1.string[] ReadConfig()
 ///该方法用于读取包含系统本地配置的XML文档,文档的名称为LocalConfig.xml。读取的结果
 ///保存在一个字符串数组中返回
 ///2.void WriteConfig(string[] localConfig)
 ///该方法将保存在字符串数组中的本地配置信息写入LocalConfig.xml文档中.
 ///3.LocalConfig.xml的格式为
 /*
   <?xml version="1.0" standalone="yes"?>
   <XmlDataSet>
   <XmlTable>
   <Col_DataBaseServer>数据库服务器名称</Col_DataBaseServer>
   <Col_Account>访问服务器帐号</Col_Account>
   <Col_Password>访问服务器的密码</Col_Password>
   <Col_DefaultDataBase>默认数据库的名称</Col_DefaultDataBase>
   <Col_UnitName>使用系统单位名称<Col_UnitName>
   <Col_SystemName>系统名称</Col_SystemName>
   </XmlTable>
   </XmlDataSet>
    */
 ///4.LocalConfig.xml文档必须在可执行文件的目录下
 ///</summary>
 public struct Struct_Param
 {
  public string ParamName;
  public System.Data.SqlDbType SQLType;
  public int Length;
  public string Value;
 }
 public class Class_DataBase
 {
  private string Str_Connection;//连接串变量
  private System.Data.SqlClient.SqlConnection Sql_Connection;//SQL连接变量
  private System.Data.SqlClient.SqlCommand Sql_Command;

  public int Int_Count;
  private static string[] Str_LocalConfig=new String[6];
  public Class_DataBase(string[] localConfig)
  {
   Str_LocalConfig=localConfig;
  }
  public bool ConnectionCheck()
  {
   Str_Connection="user id="+Str_LocalConfig[1].Trim ()+";password="+Str_LocalConfig[2].Trim() +
    ";datasource=\""+Str_LocalConfig[0].Trim()+"\";persist security info=False;inital catalog="+Str_LocalConfig[3].
       Trim();
   Sql_Connection=new System.Data.SqlClient.SqlConnection(Str_Connection);
   try
   {
    Sql_Connection.Open();//测试连接
    Sql_Connection.Close();

    return true;//设置测试结果
   }
   catch
   {
    return false;//设置测试结果
   }

  }
  public bool ConnectionCheck(string[] localConfig)
  {
   Str_Connection="user id="+localConfig[1].Trim ()+";password="+localConfig[2].Trim() +";datasource=\""+localConfig[0].Trim()+"\";persist security info=False;inital catalog="+localConfig[3].Trim();
   Sql_Connection=new System.Data.SqlClient.SqlConnection(Str_Connection);
   try
   {
    Sql_Connection.Open();//测试连接
    Sql_Connection.Close();
                
    return true;//设置测试结果
   }
   catch
   {
    return false;//设置测试结果
   }
  }

  public int ExecsSQLCommand(string SQLCommand)
  {
   Str_Connection="user id="+Str_LocalConfig[1].Trim ()+";password="+Str_LocalConfig[2].Trim() +";datasource=\""+Str_LocalConfig[0].Trim()+"\";persist security info=False;inital catalog="+Str_LocalConfig[3].
       Trim();
   Sql_Connection=new System.Data.SqlClient.SqlConnection(Str_Connection);
   Sql_Connection.Open();
   Sql_Command=new System.Data.SqlClient.SqlCommand();
   Sql_Command.Connection=Sql_Connection;
   Sql_Command.CommandType=System.Data.CommandType.Text;
   Sql_Command.CommandText=SQLCommand;
   Int_Count=Sql_Command.ExecuteNonQuery();
   Sql_Connection.Close();
            
   return Int_Count;
  }
  public System.Data.SqlClient.SqlDataReader ExecSQL(string SQLCommand)
  {
   System.Data.SqlClient.SqlDataReader RR;

   Str_Connection="user id="+Str_LocalConfig[1].Trim ()+";password="+Str_LocalConfig[2].Trim() +
    ";datasource=\""+Str_LocalConfig[0].Trim()+"\";persist security info=False;inital catalog="+Str_LocalConfig[3].
       Trim();
   Sql_Connection=new System.Data.SqlClient.SqlConnection(Str_Connection);
   Sql_Connection.Open();
   Sql_Command=new System.Data.SqlClient.SqlCommand();
   Sql_Command.Connection=Sql_Connection;
   Sql_Command.CommandType=System.Data.CommandType.Text;
   Sql_Command.CommandText=SQLCommand;
   RR=Sql_Command.ExecuteReader();
   Sql_Command.Dispose();
   return RR;
  }
        
  public System.Data.SqlClient.SqlDataReader ExecSQLProcess(string ProcName,Struct_Param[] Parm)
  {
   Str_Connection="user id="+Str_LocalConfig[1].Trim ()+";password="+Str_LocalConfig[2].Trim() +
    ";datasource=\""+Str_LocalConfig[0].Trim()+"\";persist security info=False;inital catalog="+Str_LocalConfig[3].
       Trim();
   Sql_Connection=new System.Data.SqlClient.SqlConnection(Str_Connection);
   Sql_Command.Connection=Sql_Connection;
   Sql_Command.CommandType=System.Data.CommandType.StoredProcedure;

   Sql_Command.CommandText=ProcName;
   for(int i=0;i<Parm.Length;i++)
   {
    System.Data.SqlClient.SqlParameter PP=Sql_Command.Parameters.Add(Parm[i].ParamName,Parm[i].
     SQLType,Parm[i].Length);
    PP.Value =Parm[i].Value;
   }

   return Sql_Command.ExecuteReader();

  }
  public void ConnectionClose()
  {
   Sql_Connection.Close();
  }

 } 


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
8,062.012ms