首页 > 代码库 > ADO.NET连接数据库

ADO.NET连接数据库

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
 
protected void Page_Load(object sender, EventArgs e)
        {
             SqlConnection conn = new SqlConnection(@"server=HP\MYSQL;uid=sa;pwd=123456;database=Book_Management_System;timeout=10");        //无密码验证模式的ConnectString为(server=HP\\SQLEXPRESS;database=binguan;Integrated Security=SSPI;timeout=5)
            try
            {
                conn.Open();        //打开连接
            }
            catch (SqlException E)
            {
                (Master.FindControl("ErrorDisplay") as Label).Text = E.Message.ToString();        //将错误显示在ErrorDisplay这个Label控件中
            }
            finally
            {
                conn.Close();       //关闭数据库连接
            }
        }