C#配置文件连接数据库了,那么登陆界面里代码怎么写???
2个回答
展开全部
不知道你的意思,你写了配置文件但你并未连接数据库,新建一个 连接
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["配置名字"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["配置名字"].ConnectionString;
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
展开全部
配置文件下,configuration节点下的connectionStrings节点下,增加节点add,例如
<add name="strConn" connectionString="连接字符串"/>
C#代码
1、项目里添加引用System.Configuration
2、类顶部添加引用using System.Configuration;
获取连接字符串代码
string connStr = ConfigurationManager.ConnectionStrings[“连接字符串名称”].ConnectionString;
这里的“连接字符串名称”指的是name的值
特别说明一下,连接字符串最好加密后再放入配置文件里
<add name="strConn" connectionString="连接字符串"/>
C#代码
1、项目里添加引用System.Configuration
2、类顶部添加引用using System.Configuration;
获取连接字符串代码
string connStr = ConfigurationManager.ConnectionStrings[“连接字符串名称”].ConnectionString;
这里的“连接字符串名称”指的是name的值
特别说明一下,连接字符串最好加密后再放入配置文件里
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询