首页 > 代码库 > c# 连接数据库

c# 连接数据库



/*
           /*
       try { string ConStr = "data source=USER-20170228GJ\\SQLEXPRESS;Initial Catalog=db_zrmex;user=sa;pwd=123;"; //数据库连接字段 SqlConnection conn = new SqlConnection(ConStr); conn.Open(); MessageBox.Show("连接数据库成功!"); } catch { MessageBox.Show("连接数据库失败!"); }
*/


       // 添加App.config文件
       /*

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Constr" connectionString="data source=USER-20170228GJ\SQLEXPRESS;Initial Catalog=db_zrmex;user=sa;pwd=123;" />
</connectionStrings>
</configuration>


        */
try { string connstr = ConfigurationManager.ConnectionStrings["Constr"].ToString(); //需要添加引用System.configuration SqlConnection conn = new SqlConnection(connstr); conn.Open(); MessageBox.Show("连接数据库成功!"); } catch { MessageBox.Show("连接数据库失败!"); }

 

c# 连接数据库