当前位置:Gxlcms > 数据库问题 > winForm连接数据库(sqlserver2005)

winForm连接数据库(sqlserver2005)

时间:2021-07-01 10:21:17 帮助过:2人阅读

xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="connString" value="Data Source=LIBL;Initial Catalog=HoverTreeSCJ;User ID=sa;Password=sa"></add> </appSettings> </configuration>
HoverTreeSCJ 为数据库的名称

3.在项目Reference中添加引用 System.configuration

在文件中 添加引用 using System.configuration;

                                using System.Data.SqlClient;

4.获取App.config中的连接字符串

    public string ConnString = System.Configuration.ConfigurationManager.AppSettings["connString"];

5.连接数据库,读取数据

SqlConnection conn = new SqlConnection(ConnString);
            string strSql = "SELECT * FROM Basetb";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = strSql;
            cmd.Connection = conn;
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    this.textBox1.Text = dr[0].ToString();
                }
            }
            conn.Close();

/* hovertree.top */

推荐:http://www.cnblogs.com/roucheng/p/DataGridView.html

winForm连接数据库(sqlserver2005)

标签:

人气教程排行