Sql/MySql 连接配置
2021-06-23 本文已影响0人
秒怂的哈士奇爱吃西瓜
config配置:
<add name="ConnectionStrings" connectionString="Data Source=192.168.3.2;port=3306;Initial Catalog=tsyw;user id=root;password=123456;Charset=utf8" providerName="MySql.Data.MySqlClient" />
<add name="ConnectionStrings" connectionString="Data Source=192.168.3.2;Initial Catalog=TSYW;User ID=sa;Password=AAbb123456;Persist Security Info =True;" providerName="System.Data.SqlClient" />
引用文件:
using System.Data
sql:using System.Data.SqlClient;
mysql:using MySql.Data.MySqlClient;
//连接信息设置-------------------------------------------------
if (DbConnects.DbType == "MySql")
{
connStr = String.Format("server={0}; database={1}; user id={2}; password={3}; pooling=false;port=3306", DbConnects.ConnectionName, DbConnects.DataName, DbConnects.UserName, DbConnects.Password);
}
else
{
connStr = String.Format("Data Source ={0}; Initial Catalog = {1}; User ID = {2}; password={3};Integrated Security = False", DbConnects.ConnectionName, DbConnects.DataName, DbConnects.UserName, DbConnects.Password);
}
//创建连接方法-------------------------------------------------
DataSet ds = new DataSet();
if (sqlType == "MySql")
{
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
MySqlDataAdapter sqlAdp = new MySqlDataAdapter(connStr, sql);
sqlAdp.Fill(ds);
return ds;
}
}
else
{
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand com = conn.CreateCommand();
com.CommandText = sql;
SqlDataAdapter sqlAdp = new SqlDataAdapter(com);
sqlAdp.Fill(ds);
}
return ds;
}