C#连接SQL Server数据库(一)

C#连接数据库:Connection对象

1.Connection对象概述

  Connection对象是一个连接对象,主要功能是建立与物理数据库的连接。其主要包括4种访问数据库的对象类,也可称为数据提供程序,分别介绍如下。
    SQL Server数据提供程序,位于System.Data.SqlClient命名空间。
    ODBC数据提供程序,位于System.Data.Odbc命名空间。
    OLEDB数据提供程序,位于System.Data.OleDb命名空间。
    Oracle数据提供程序,位于System.Data.OracleClient命名空间。

说明:根据使用数据库的不同,引入不同的命名空间,然后通过命名空间中的Connection对象连接类连接数据库。例如,连接SQL Server数据库,首先要通过using System.Data.SqlClient命令引用SQL Server数据提供程序,然后才能调用空间下的SqlConnection类连接数据库。


2.连接数据库

  以SQL Server数据库为例,如果要连接SQL Server数据库,必须使用System.Data.SqlClient命名空间下的SqlConnection类。所以首先要通过using System.Data.SqlClient命令引用命名空间,连接数据库之后,通过调用SqlConnection对象的Open方法打开数据库。通过SqlConnection对象的State属性判断数据库的连接状态。

界面:

代码:
        private void btn1_Click(object sender, EventArgs e)
        {
            if (txt1.Text == "")
            {
                MessageBox.Show("请输入要连接的数据库名称!");
            }
            else
            {
                try
                {
                    string connString = "server=.;database=" + txt1.Text.Trim() + ";uid=test;pwd=test;connect timeout=5"; //**
                    SqlConnection sqlConnection = new SqlConnection(connString); //**
                    sqlConnection.Open(); //**

                    if (sqlConnection.State == ConnectionState.Open)
                    {
                        lab2.Text = "数据库【" + txt1.Text.Trim() + "】已经连接并打开!";
                    }
                }
                catch
                {
                    MessageBox.Show("数据库连接失败!");
                }
            }
        }

3.关闭连接

  当对数据库操作完毕后,要关闭与数据库的连接,释放占用的资源。可以通过调用SqlConnection对象的Close方法或Dispose方法关闭与数据库的连接。这两种方法的主要区别是:Close方法用于关闭一个连接,而Dispose方法不仅关闭一个连接,而且还清理连接所占用的资源。当使用Close方法关闭连接后,可以再调用Open方法打开连接,不会产生任何错误。而如果使用Dispose方法关闭连接,就不可以直接用Open方法打开连接,必须再次重新初始化连接再打开。

界面:

代码:

        SqlConnection sqlConnection; //***
        /// <summary>
        /// 连接数据库
        /// </summary>
        private void btn1_Click_1(object sender, EventArgs e)
        {
            if (txt1.Text == "")
            {
                MessageBox.Show("请输入数据库名称:");
            }
            else
            {
                try
                {
                    string connString = "server=.;database=" + txt1.Text.Trim() + ";uid=test;pwd=test;connect timeout=5"; //***
                    sqlConnection = new SqlConnection(connString); //***
                    sqlConnection.Open(); //***

                    if (sqlConnection.State == ConnectionState.Open)
                    {
                        btn1.Text = "连接成功";
                    }
                }
                catch(Exception ex) 
                {
                    MessageBox.Show(ex.Message);
                    txt1.Text = "";
                }
            }
        }

        /// <summary>
        /// 使用Close方法关闭连接并重新调用Open方法连接数据库
        /// </summary>
        private void btn2_Click(object sender, EventArgs e)
        {
            try
            {
                string str = "";

                sqlConnection.Close(); //***
                if (sqlConnection.State == ConnectionState.Closed)
                {
                    str = "数据库已经成功关闭\n";
                }

                sqlConnection.Open(); //***
                if (sqlConnection.State == ConnectionState.Open)
                {
                    str += "数据库已经成功打开\n";
                }

                rtbox1.Text = str;
            }
            catch (Exception ex)
            {
                rtbox1.Text = ex.Message;
            }
        }

        /// <summary>
        /// 使用Dispose方法关闭连接并重新调用Open方法连接数据库
        /// </summary>
        private void btn3_Click(object sender, EventArgs e)
        {
            try
            {
                sqlConnection.Dispose(); //***
                sqlConnection.Open(); //***
            }
            catch (Exception ex)
            {
                rtbox1.Text = ex.Message;
            }
        }
发布了44 篇原创文章 · 获赞 29 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/IT_xiao_guang_guang/article/details/104123315