当前位置:编程学习 > C#/ASP.NET >>

.net2005连接数据库

using System;
using System.Collections.Generic;
using System.Text;


namespace WindowsApplication9
{
    public class Class1
    {
        public Class1()
        {

        }
        string strConn = "server=192.168.1.1;Initial Catalog=test;Integrated Security=True";
        public void ma(){
        
        
            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn))
            {
                conn.Open();
                using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand())
                {
                    command.CommandText = "select * from table1";
                    using (System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(command))
                    {
                        System.Data.DataSet ds = new System.Data.DataSet();
                        adp.Fill(ds);
                        dataGridView1.DataSource = ds.Tables[0];
                }
                
                conn.Close();
            }
        }
    }
    }
}

        
        
        


    

高手来帮菜鸟找错! --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Text;


namespace WindowsApplication9
{
    public class Class1
    {
        public Class1()
        {

        }
        string strConn = "server=192.168.1.1;Initial Catalog=test;Integrated Security=True";
        public void ma(){
       
       
            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn))
            {
                conn.Open();
                using (System.Data.SqlClient.SqlCommand command = conn.CreateCommand())
                {
                    command.CommandText = "select * from table1";
                    using (System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(command))
                    {
                        System.Data.DataSet ds = new System.Data.DataSet();
                        adp.Fill(ds);
                        dataGridView1.DataSource = ds.Tables[0];
                        dataGridView1.DataBind();
                }
               
                conn.Close();
            }
        }
    }
    }
}
--------------------编程问答-------------------- 说什么上下文不存在dataGridView1。该在那里写什么?
--------------------编程问答--------------------
dataGridView1.DataBind();

如果是WEBFORM需加此一句话 --------------------编程问答-------------------- 还不对哦。。说什么上下文不存在名称“dataGridView1
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,