訪問Mysql資料庫,連線字串配置
阿新 • • 發佈:2018-12-01
Step1:App.config檔案裡進行配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<add name="Connect" connectionString="server=localhost;user=root;pwd=admin;database=BlackCat"/>
</connectionStrings>
</configuration>
Step2:新增引用
Step3:使用
public class MySqlHelp { public static string connectString = ConfigurationManager.ConnectionStrings["Connect"].ConnectionString.ToString(); /// <summary> /// 執行sql語句返回受影響的行數 /// </summary> /// <param name="sql"></param>/// <returns></returns> public static int ExecuteSql(string sql) { int count; using (MySqlConnection con = new MySqlConnection(connectString)) { using (MySqlCommand cmd = new MySqlCommand(sql, con)) {try { con.Open(); count = cmd.ExecuteNonQuery(); return count; } catch(MySqlException e) { con.Close(); throw e; } } } } }