使用Connection对象连接数据库
第一种连接access数据库:[使用OleDbConnection对象连接Ole数据源]
1)在web.config页面中</configSections>脚本下写代码:
<appSettings>
<add key="AccessConnectionString" value="Provider = Microsoft.jet.OLEDB.4.0;Jet OLEDB:Database Password=yxb x%E$ ^GF;Data Source =" />
<add key="Accessmapp" value="~/App_Data/SSQDYJAAA.mdb"/>
</appSettings>2)创建connDb.cs类,在给类中写代码:
using System.Data;
using System.Data.OleDb;(不要忘记写)
public static OleDbConnection Getconn()
{
string olestr = System.Configuration.ConfigurationManager.AppSettings["AccessConnectionString"].ToString()
+ HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["Accessmapp"].ToString());
OleDbConnection myconn = new OleDbConnection(olestr);
return myconn;
}3)页面应用:
using System.Data;
using System.Data.OleDb;(不要忘记写)
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection myConn = connDb.Getconn(); //设置连接
if (myConn.State == ConnectionState.Closed)
{
myConn.Open(); //如果连接未打开,则进行处理
}
string sql = "SELECT [LotIssue], [StartTime], [Result], [Result2] FROM [FL_Issue]";
DataSet myset = new DataSet();
OleDbDataAdapter mydata = new OleDbDataAdapter(sql, myConn);
mydata.Fill(myset);
GridView1.DataSource = myset.Tables[0];
//Response.Write(myset.Tables[0].Rows.Count);
GridView1.DataBind();
}第二种连接mysql2005数据库【使用SqlConnection连接SQL Server数据库】:准备工作是需要创建mysql2005数据库,还需要为sa设置登录密码【1 打开SQL Server企业管理器。2 选择服务器名称上右键选择“编辑SQL Server注册属性”,然后在对话框中选择“使用windows身份验证”。5 在选项卡中,选择身份验证为“SQL Server和 Windows ,设置好后需要重新启动服务。】
1)在web.config页面中</configSections>脚本下写代码:
<appSettings>
<add key="SQL2005ConnectionString" value=" Server=数据库服务器名称\SQLEXPRESS;database=msql2005Test;uid=sa;password=1234567890"/></appSettings>
2)创建connDb.cs类,在给类中写代码:
using System.Data;
using System.Data.SqlClient;(不要忘记写)
public static SqlConnection Getconn()
{
string sqlstr = System.Configuration.ConfigurationManager.AppSettings["SQL2005ConnectionString"].ToString();
SqlConnection myconn = new SqlConnection(sqlstr);
return myconn;
}3)页面应用:
using System.Data;
using System.Data.SqlClient;(不要忘记写)
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConn = connDb.Getconn(); //设置连接
if (myConn.State == ConnectionState.Closed)
{
myConn.Open(); //如果连接未打开,则进行处理
Response.Write("数据库连接状态是:"+myConn.State);
}
}
第三种连接mysql2005数据库【使用OleDbConnection连接SQL Server数据库】:
1)在web.config页面中</configSections>脚本下写代码: <appSettings>
<add key="OleDbSQL2005ConnectionString" value=" provider=SQLOLEDB; Data Source=数据库服务器名称\SQLEXPRESS;Initial Catalog=msql2005Test;User Id=sa;Pwd=1234567890"/></appSettings>
2)创建connDb.cs类,在给类中写代码:
using System.Data;
using System.Data.OleDb;(不要忘记写)
public static OleDbConnection OleDbConnSqlServer2005(){
string sqlstr = System.Configuration.ConfigurationManager.AppSettings["OleDbSQL2005ConnectionString"].ToString();
OleDbConnection myconn = new OleDbConnection(sqlstr);
return myconn;3)页面应用:
using System.Data;
using System.Data.OleDb;(不要忘记写)
protected void Page_Load(object sender, EventArgs e)
{OleDbConnection myConn = Class1.OleDbConnSqlServer2005(); //设置连接
if (myConn.State == ConnectionState.Closed)
{
myConn.Open(); //如果连接未打开,则进行处理
}
string sql = "SELECT * FROM [TestTable]";
DataSet myset = new DataSet();
OleDbDataAdapter mydata = new OleDbDataAdapter(sql, myConn);
mydata.Fill(myset);
GridView1.DataSource = myset.Tables[0];
//Response.Write(myset.Tables[0].Rows.Count);
GridView1.DataBind();
}
asp.net如何连接各类数据库?
最新推荐文章于 2024-02-28 15:54:49 发布