1、在项目根目录下添加应用程序配置文件App.config。
2、在App.config中添加节点。
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <connectionStrings> 4 <add name="dbconStr" connectionString="Data Source=.;Initial Catalog=OCTOP;User ID=sa;Password=1"/> 5 </connectionStrings> 6 </configuration>
3、在项目中添加对System.Configuration的引用。
4、再程序中读取配置文件
1 string connStr = ConfigurationManager.ConnectionStrings["dbconStr"].ConnectionString; 2 3 using (SqlConnection conn = new SqlConnection(connStr)) 4 { 5 conn.Open(); 6 using (SqlCommand cmd = conn.CreateCommand()) 7 { 8 cmd.CommandText = "select count(*) from bdWLDA "; 9 int i = (int)cmd.ExecuteScalar(); 10 11 MessageBox.Show(i.ToString()); 12 } 13 }
                    
                
                
            
        
浙公网安备 33010602011771号