一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net SqlConnection连接数据库详解

时间:2022-06-25 06:04:25 编辑:袖梨 来源:一聚教程网

数据库教程连接字符串,必要的如下:
New   SqlConnection( "Server=LocalHost;DataBase=Pubs;UID=sa;PWD=123 ")

下面先看个实例

SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=(local);Initial Catalog=news;User ID=sa;Password=123";

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;

        for (int i=0;i<=GridView1.Rows.Count-1;i++)
        {
           CheckBox cbox=(CheckBox)GridView1.Rows [i].FindControl ("CheckBox1");
           if (cbox.Checked == true)
           {
             
               cmd.CommandText = "update 主题表 set 审核='已审核' where 主帖编号="+GridView1 .DataKeys[i].Value;
               conn.Open();
               cmd.ExecuteReader();
               conn.Close();
           }
       
        }

我们对上面实例一句句解析说明

1、建立一个数据库连接对象 conn
2、指定欲连接的数据库参数:服务器为本地服务器,数据库名'news’,用户ID为'sa',密码为'123'
3、建立一个Command对象 cmd
4、将cmd的Connection变量赋值为第1步建立的对象conn
5、for循环遍历GridView1控件里的每一行
6、获得GridView1第i行里的‘CheckBox1’属性
7、判断CheckBox是否选中
8、为cmd指定执行内容,更新数据库‘主题表’里‘主帖编号’="+GridView1 .DataKeys[i].Value的那一行里‘审核’字段内容为‘已审核’
9、连接数据库
10、执行第8步指定的内容
11、关闭数据库连接

好了综合上述我们写个完整的实例

<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.Data.Odbc" %>





   


       

Properties from the MSDE Connection
       


       

       


       


       

From SqlConnection
       


       
           
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
           
       

                        ConnectionString:
                   

                        <asp教程:Label id="lblSqlConnectionString" runat="server">ConnectionString

                        ConnectionTimeout:
                   

                        ConnectionTimeout

                        Database:
                   

                        Database

                        DataSource:
                   

                        DataSource

                        PacketSize:
                   

                        PacketSize

                        ServerVersion:
                   

                        ServerVersion

                        State:
                   

                        State

                        WorkstationId:
                   

                        WorkstationId

       


       


       

From OleDbConnection
       


       
           
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
           
       

                        ConnectionString:
                   

                        ConnectionString

                        ConnectionTimeout:
                   

                        ConnectionTimeout

                        Database:
                   

                        Database

                        DataSource:
                   

                        DataSource

                        Provider:
                   

                        Provider

                        ServerVersion:
                   

                        ServerVersion

                        State:
                   

                        State

       


       


       

From OdbcConnection
       


       
           
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
               
                   
                   
               
           
       

                        ConnectionString:
                   

                        ConnectionString

                        ConnectionTimeout:
                   

                        ConnectionTimeout

                        Database:
                   

                        Database

                        DataSource:
                   

                        DataSource

                        Driver:
                   

                        Driver

                        ServerVersion:
                   

                        ServerVersion

                        State:
                   

                        State

       


       


   


热门栏目