zoukankan      html  css  js  c++  java
  • asp.net连接数据库

    Asp.net web连接数据库步骤。

    一、      新建一个web工程。

          1.文件->添加->新建网站->asp.net web网站Winform窗体。

         2.新建好的网站最下面有个web.config文件,在个文件里面找到数据库连接字符串,如下:

    <connectionStrings>

          <add name="DefaultConnection" connectionString="

    Data Source=PC-20160403SKQWSQLEXPRESS;Initial Catalog=CAD;uid=sa;pwd=123456;Integrated Security=True" providerName="System.Data.SqlClient" />

    </connectionStrings>

         3.其中Data Source是数据库实例名称,如果是网络数据库则Data Source=ip,端口;例如Data Source= 192.1608.68.12,2000;

    如果是本地用户的话,Data Source=数据库实例名称;例如Data Source= PC-20160403SKQWSQLEXPRESS;可以在登陆数据数据库的时候看到实例名称,如下图:

     

                4. Initial Catalog=数据库名称;uid=登陆账户;pwd=密码;Integrated Security=True

    二、      代码实现读取。

    新建一个winform窗体test.aspx,在test.aspx.cs里面编写代码

    using System.Configuration;

    using System.Data.SqlClient;

    protected void Page_Load(object sender, EventArgs e)

            {

                //1.获取配置文件里面配置的数据库连接字符串

                String connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();

                SqlConnection connection =new SqlConnection(connectionString);

                // Create the Command and Parameter objects.

               

                               //2.sql语句

                string queryString =

                "SELECT * from Solution";

               

              

                SqlCommand command = new SqlCommand(queryString, connection);

                // Open the connection in a try/catch block.

                // Create and execute the DataReader, writing the result

                // set to the console window.

              //3.打开连接

                    connection.Open();

               //4.执行sql语句

                    SqlDataReader reader = command.ExecuteReader();

               //5.查看返回结果

                    while (reader.Read())

                    {

                        //Console.WriteLine(" {0} {1} {2}",

                        //    reader[0], reader[1], reader[2]);

                        String a = reader[0].ToString();

                        Console.WriteLine(a);

                    }

                 //6.关闭连接

                    reader.Close();

           }

  • 相关阅读:
    ylbtech-dbs-m-YinTai(银泰网)
    ylbtech-memorandum(备忘录)-数据库设计
    设计工作-Axure
    RegexHelper
    convert image to base64
    MVC Movie App
    ASP.NET MVC file download sample
    MVC架构、WebForm与MVC对比
    第2章 数字之魅——子数组之和的最大值(二维)
    第2章 数字之魅——求数组的子数组之和的最大值
  • 原文地址:https://www.cnblogs.com/gongpipi/p/5426724.html
Copyright © 2011-2022 走看看