没这样做过,我一般做的dll连接串一般是由一个口径统一传进去的.
不过WINFORM 也同样存在一个config.你的想法应该可以实现.

解决方案 »

  1.   

    和ASP.Net一样的,可以将配置信息存储在config文件里。读取方法也一样。
      

  2.   

    to alexy: but how to solve the security problem? anybody may see the password of sa.
      

  3.   

    to namhyuk(namhyuk) 
    密码可以加密的to 楼主
    可以的,Duwamish中有例子,看看吧
      

  4.   

    应该在应用程序(web.config或xxx.exe.config)中保存。不需要为dll做一个config文件(或INI或XML)来保存数据库连接字符串,因为象楼主说的那样其实是封装的更不好了而不是“对数据访问进行更好的封装”(比方说你要发布这个组件给别人重用,除了组件本身,你还得发布相应的配置文件,还得想办法说明配置文件的格式、含义和配置方法。)
      

  5.   

    Duwamish中的例子好像比较复杂,哪位能给个比较简明的例子,谢谢!
      

  6.   

    可以写在.Config的配置文件中你也可以自己写一个XML 文件,同样可以保存这个字串(用dataset.readXml() 读取)也可用.ini文件,也可以用txt文件,到时候用filestream 对象读取就是了
      

  7.   

    zhongwanli(想一下,就会可以) 先生,能给个例子吗?
       比如说这个DLL(类库)项目和一个Web应用程序项目在同一个Solution里,那么怎样让这个访问数据库的DLL利用到Web应用程序项目中的Web.Config文件呢?
       再一次感谢!!!!!(我是新手,请多指教)
      

  8.   

    我是放在XML文件中的,还专门为配置文件写了个类
      

  9.   

    虽然说WinForm程序可以写在App.Config文件中但如果能自己写一个Dll从.ini文件或其他文件中读取数据库连接信息这样更好,从理论上讲这肯定是可行的,不过我没写过,所以如果哪位大吓能提供一个简单的例子,小弟不胜感激!
      

  10.   

    StreamReader sreader=new StreamReader(Application.StartupPath +"\\"+"Xml_Connstr.Xml");
    XmlDataDocument xdoc=new XmlDataDocument();
    xdoc.DataSet.ReadXml(sreader);
    DataSet dsxml=xdoc.DataSet;Base.DataSource=dsxml.Tables[0].DefaultView[0][2].ToString();
    Base.InitalCatalog=dsxml.Tables[0].DefaultView[0][3].ToString();
    Base.loginId=dsxml.Tables[0].DefaultView[0][4].ToString();
    Base.password=dsxml.Tables[0].DefaultView[0][5].ToString();
    read=true;
    //---组成连接字串
    Base.Connectstring+="Persist Security Info="+dsxml.Tables[0].DefaultView[0][1].ToString()+";";
    Base.Connectstring+="Data Source="+ dsxml.Tables[0].DefaultView[0][2].ToString()+";";
    Base.Connectstring+="Initial Catalog="+dsxml.Tables[0].DefaultView[0][3].ToString()+";";
    Base.Connectstring+="User Id="+dsxml.Tables[0].DefaultView[0][4].ToString()+";";
    Base.Connectstring+="Password="+dsxml.Tables[0].DefaultView[0][5].ToString()+";";xdoc=null;
    sreader.Close();
    注:这是我的公共类(Base)中的一代码,这是用读取xml 文件用的
    字串,下面是一个简单的xml 文件
      

  11.   

    <?xml version="1.0" standalone="yes" ?> 
    - <Connstr>
      <Provider>SqlOledb.1</Provider> 
      <Persist>false</Persist> 
      <DataSource>ds0390</DataSource> 
      <InitialCatalog>ESD_DB</InitialCatalog> 
      <UserID>sa</UserID> 
      <Password></Password> 
      </Connstr>
      

  12.   

    我对XML文件不是很熟。但我想问zhongwanli,如果像这样子那么谁都可以看到这个.xml文件,并且取得sql server的sa密码。这方面有什么解决办法吗?
      

  13.   

    可以,没问题,自己写个XML加密密码也行
      

  14.   

    xml可以加密密码?干脆写个例子好不?我最近真的恰好用得上.另外,我刚刚想到,在.resources文件里存ConnString也应该可以的.不过,不太好编辑.
      

  15.   

    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="pToEncrypt"></param>
    /// <param name="sKey"></param>
    /// <returns></returns>
    public string  Encrypt(string  pToEncrypt,  string  sKey)  
    {  
    DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider();  
    //把字符串放到byte数组中  
    //原来使用的UTF8编码,我改成Unicode编码了,不行  
    byte[]  inputByteArray  =  Encoding.Default.GetBytes(pToEncrypt);  
    //byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);  
     
    //建立加密对象的密钥和偏移量  
    //原文使用ASCIIEncoding.ASCII方法的GetBytes方法  
    //使得输入密码必须输入英文文本  
    des.Key  =  ASCIIEncoding.ASCII.GetBytes(sKey);
    des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey);
    MemoryStream  ms  =  new  MemoryStream();  
    CryptoStream  cs  =  new  CryptoStream(ms,  des.CreateEncryptor(),CryptoStreamMode.Write);  
    //Write  the  byte  array  into  the  crypto  stream  
    //(It  will  end  up  in  the  memory  stream)  
    cs.Write(inputByteArray,  0,  inputByteArray.Length);  
    cs.FlushFinalBlock();  
    //Get  the  data  back  from  the  memory  stream,  and  into  a  string  
    StringBuilder  ret  =  new  StringBuilder();  
    foreach(byte  b  in  ms.ToArray())  
    {  
    //Format  as  hex  
    ret.AppendFormat("{0:X2}",  b);  
    }  
    ret.ToString();  
    return  ret.ToString();  
    }
    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="pToDecrypt"></param>
    /// <param name="sKey"></param>
    /// <returns></returns>
    public string Decrypt(string pToDecrypt,  string sKey)  
    {  
    DESCryptoServiceProvider  des  =  new  DESCryptoServiceProvider();  
     
    //Put  the  input  string  into  the  byte  array  
    byte[]  inputByteArray  =  new  byte[pToDecrypt.Length  /  2];  
    for(int  x  =  0;  x  <  pToDecrypt.Length  /  2;  x++)  
    {  
    int  i  =  (Convert.ToInt32(pToDecrypt.Substring(x  *  2,  2),  16));  
    inputByteArray[x]  =  (byte)i;  
    }  
     
    //建立加密对象的密钥和偏移量,此值重要,不能修改  
    des.Key  =  ASCIIEncoding.ASCII.GetBytes(sKey);  
    des.IV  =  ASCIIEncoding.ASCII.GetBytes(sKey);  
    MemoryStream  ms  =  new  MemoryStream();  
    CryptoStream  cs  =  new  CryptoStream(ms,  des.CreateDecryptor(),CryptoStreamMode.Write);  
    //Flush  the  data  through  the  crypto  stream  into  the  memory  stream  
    cs.Write(inputByteArray,  0,  inputByteArray.Length);  
    cs.FlushFinalBlock();  
     
    //Get  the  decrypted  data  back  from  the  memory  stream  
    //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象  
    StringBuilder  ret  =  new  StringBuilder();  
                 
    return  System.Text.Encoding.Default.GetString(ms.ToArray());  
    }