求用ASP.NET C#编写的学生信息管理系统,需要连接SQL数据库,急用,请大家帮帮忙,不胜感激!!!
2个回答
展开全部
//以下是web.config中连接字符串配置节
<appSettings>
<!--数据库连接-->
<add key="ConnStr" value="Data Source=服务器IP,默认为:(local);Initial Catalog=数据库名;User ID=登录数据库用户名;Password=登录密码"/>
</appSettings>
//以下是连接方法
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// </summary>
public class DBConn
{
private SqlConnection _conn;
public DBConn()
{
_conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
}
public void Open()
{
if (_conn.State == ConnectionState.Closed)
{ _conn.Open(); }
}
public void Close()
{
if (_conn.State == ConnectionState.Open)
{
_conn.Close();
_conn.Dispose();
}
}
}
<appSettings>
<!--数据库连接-->
<add key="ConnStr" value="Data Source=服务器IP,默认为:(local);Initial Catalog=数据库名;User ID=登录数据库用户名;Password=登录密码"/>
</appSettings>
//以下是连接方法
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// </summary>
public class DBConn
{
private SqlConnection _conn;
public DBConn()
{
_conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
}
public void Open()
{
if (_conn.State == ConnectionState.Closed)
{ _conn.Open(); }
}
public void Close()
{
if (_conn.State == ConnectionState.Open)
{
_conn.Close();
_conn.Dispose();
}
}
}
追问
有没有完整的程序呢?3Q
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询