C# 简单连接数据库并执行SQL查询语句

时间:2015-01-04 22:46:32   收藏:0   阅读:292

留着备用

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=.;Password=123456;User ID=sa;Initial Catalog=guo;database=guo";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from dbo.[user]";
            SqlDataReader reader = cmd.ExecuteReader();
            
            while (reader.Read())
            {
                Object[] values = new Object[reader.FieldCount];
                int nCount = reader.GetValues(values);
                for (int i = 0; i != nCount; ++i)
                    listBox1.Items.Add(values[i]);
            }
                //             while (reader.Read())
                //             {
                //                     listBox1.Items.Add(reader[1]);
                //             }
                conn.Close();
        }

 

原文:http://www.cnblogs.com/guobbs/p/4202245.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!