adandelion

诗词在线 http:/www.chinapoesy.com


诗词在线 |唐诗|宋词|元曲|诗经|离骚|乐府|现代诗歌

博客园 首页 联系 订阅 管理

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;


namespace Sx_Mdi
{

 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 public class IniFile
 {
  //文件INI名称
  public string Path;

  ////声明读写INI文件的API函数
  [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);


  //类的构造函数,传递INI文件名
  public IniFile(string inipath)
  {
   //
   // TODO: Add constructor logic here
   //
   Path = inipath;
  }

  //写INI文件
  public void IniWriteValue(string Section,string Key,string Value)
  {
   WritePrivateProfileString(Section,Key,Value,this.Path);

  }

  //读取INI文件指定
  public string IniReadValue(string Section,string Key)
  {
   StringBuilder temp = new StringBuilder(255);
   int i = GetPrivateProfileString(Section,Key,"",temp,255,this.Path);
   return temp.ToString();

  }


 }
}

操作范例:

public static SqlConnection MyConnection()
{
 string sPath;
 string ServerName,userId,sPwd,DataName;

 sPath = GetPath();
 IniFile ini = new IniFile(sPath);
 ServerName = ini.IniReadValue ("Database","server");
 userId = ini.IniReadValue ("Database","uid");
 sPwd = ini.IniReadValue ("Database","pwd");
 DataName = ini.IniReadValue ("Database","database");
 string strSql = "server =" + ServerName+";uid ="+ userId +";pwd =;database ="+ DataName;
    SqlConnection myConn=new SqlConnection(strSql);
    return myConn;
}

http://www.chinaaspx.com/article/csharp/298.htm

posted on 2006-01-11 16:01  猪头  阅读(672)  评论(0)    收藏  举报

欢迎访问诗词在线http://www.chinapoesy.com   诗词在线 |唐诗|宋词|元曲|诗经|离骚|乐府|古典诗歌|现代诗歌|古典诗词|现代诗词|诗歌可以陶冶你的情操、丰富你的生活,让你更具内涵。诗词在线打造中国最好的诗词社区!

诗词在线社区

126在线阅读网 历史书籍、文学书籍、小说。。。