App.config的配置: 数据库连接的是云服务器
<appSettings>
<!-- 连接字符串是否加密 -->
<add key="ConStringEncrypt" value="false" />
<!-- 数据库连接字符串,(如果采用加密方式,上面一项要设置为true;加密工具,可在官方下载,
如果使用明文这样server=127.0.0.1;database=.....,上面则设置为false。 -->
<!--<add key="ConnectionString" value="server=119.29.37.246
;database=Serious;uid=sa;pwd=0377123456" />-->
<add key="ConnectionString" value="server=120.79.183.167
;database=StoreHouse;uid=sa;pwd=BAILE@123456" />
<!--其它模块连接字符串,可以不断增加以便同一个项目支持连接多个数据库。如果没有,可以删除该行-->
<!--<add key="ConnectionString2" value="server=.;database=Serious;uid=sa;pwd=123456" />-->
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
//数据库连接字符串(web.config来配置),多数据库可使用DbHelperSQLP来实现.
public static string connectionString = PubConstant.ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
得到数据库的连接语句的方法:
public class PubConstant
{
/// <summary>
/// 获取连接字符串
/// </summary>
public static string ConnectionString
{
get
{
string _connectionString = ConfigurationManager.AppSettings["ConnectionString"];
string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
if (ConStringEncrypt == "true")
{
_connectionString = DESEncrypt.Decrypt(_connectionString);
}
return _connectionString;
}
}
/// <summary>
/// 得到web.config里配置项的数据库连接字符串。
/// </summary>
/// <param name="configName"></param>
/// <returns></returns>
public static string GetConnectionString(string configName)
{
string connectionString = ConfigurationManager.AppSettings[configName];
string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
if (ConStringEncrypt == "true")
{
connectionString = DESEncrypt.Decrypt(connectionString);
}
return connectionString;
}
}