using System;
using System.Data;
using System.Data.OleDb;namespace pdvi
{
public class Pdvi
{
OleDbConnection OleDbConn = new OleDbConnection();
OleDbDataAdapter OleDbAdap = new OleDbDataAdapter();
string connstr = null;
string connuid = null;
string connpwd = null;
bool Statu = false; //数据源状态标志默认是关闭 public Pdvi() //构造函数
{
} public Pdvi(string ConnStr)
{
initialize(ConnStr); //调用初始化私有方法
} private void initialize(string ConnStr) //初始化私有方法
{
OpenConnString(ConnStr); //调用数据访问方法
} private void OpenConnString(string ConnStr) //调用方法打开数据源
{
try
{
this.OleDbConn.ConnectionString = ConnStr.ToString();
this.OleDbConn.Open();
}
catch(Exception e)
{
throw e;
}
finally
{
if (this.OleDbConn.State == System.Data.ConnectionState.Open)
{
this.Statu = true; //数据源被打开标志
}
else
{
this.Statu = false;
}
}
} public void OpenConnString() //调用方法打开数据源
{
try
{
this.OleDbConn.ConnectionString = this.connstr.ToString();
this.OleDbConn.Open();
}
catch(Exception e)
{
throw e;
}
finally
{
if (this.OleDbConn.State == System.Data.ConnectionState.Open)
{
this.Statu = true; //数据源被打开标志
}
else
{
this.Statu = false;
}
}
} public void SetConnStr(string connstr) //设置连接数据源的字符串
{
this.connstr = connstr;
} public void SetConnUid(string connuid) //设置连接数据源的用户名
{
this.connuid = connuid;
} public void SetConnPwd(string connpwd) //设置连接数据源的通行码
{
this.connpwd = connpwd;
} public string GetConnStr() //获取连接数据源的字符串
{
return this.connstr;
} public string GetConnUid() //获取连接数据源的用户名
{
return this.connuid;
} public string GetConnPwd() //获取连接数据源的通行证
{
return this.connpwd;
} public OleDbDataReader GetDataReader(string sSQL)
{
OleDbCommand cmd = new OleDbCommand(sSQL,this.OleDbConn); try
{
if (this.Statu == true)
{
return cmd.ExecuteReader();
}
else
{
return null;
}
}
catch (Exception e)
{
throw e;
}
finally
{
cmd.Dispose();
this.OleDbConn.Close();
this.OleDbConn.Dispose();
}
} public DataSet GetDataSet(string sSQL)
{
this.OleDbAdap = new OleDbDataAdapter(sSQL,this.OleDbConn);
DataSet Ds = new DataSet(); try
{
if (this.Statu == true)
{
this.OleDbAdap.Fill(Ds);
return Ds;
}
else
{
return null;
}
}
catch (Exception e)
{
throw e;
}
finally
{
this.OleDbConn.Close();
this.OleDbAdap.Dispose();
}
} }
}

解决方案 »

  1.   

    System.Data.SqlClient.SqlConnection conn;
    conn= new SqlConnection("data source=sunny;user id=code;pwd=code;initial catalog=test;");
    conn.Open();
    SqlCommand dcmd=new SqlCommand("select * from test",conn);
    DataSet ds= new DataSet();
    SqlDataAdapter dadp=new SqlDataAdapter(dcmd);
    dadp.Fill(ds,"test");
                                DataGrid1.DataSource=ds.Tables["test"];
    DataGrid1.DataBind();
    conn.Close();
      

  2.   

    SqlConnection myCNN = new SqlConnection();// 构造正确的连接字符串
    myCNN.ConnectionString = "data source=.;initial catalog=aa;user id=sa;password=123;";myCNN.Open();SqlCommand myCMD = new SqlCommand();myCMD.Connection = myCNN;
    myCMD.CommandText = "Select Count(*) From tPassword Where cPassword = '" + strGroupPassword + "'";if ((int)myCMD.ExecuteScalar() == 0)
    return "ERROR";
    DATAGRID用DATASET填充
      

  3.   

    SqlConnection nwindConn = new SqlConnection("Data Source=mysource;user id=user;password=;Initial Catalog=northwind"); SqlCommand readCMD = nwindConn.CreateCommand();
    readCMD.CommandText = "SELECT * FROM userinfo"; nwindConn.Open(); SqlDataReader myReader = readCMD.ExecuteReader(); while (myReader.Read())
    {
    Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
    } myReader.Close();
    nwindConn.Close();
      

  4.   

    在CS结构中,DATAGRID是怎样和一个表绑定的?
    如:
    dgd.DataSource=ds.Tables(0);
    但这样不对呀????
    急急急!!!!!!!!!!!!!!!
    谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  5.   


    DataSet ds = new DataSet();SqlDataAdapter adapt = new SqlDataAdapter();
    adapt.SelectCommand = showCMD;adapt.Fill(ds);
    DataGrid.DataSource = ds.Tables["tablename"];
    DataGrid.DataBind();
      

  6.   

    System.Data.Odbc.OdbcConnection conn=new OdbcConnection("DSN=gd;");
    System.Data.Odbc.OdbcCommand cmd =new OdbcCommand("select * from person",conn);
    System.Data.DataSet ds1=new DataSet (); 
    //System.Data.DataTable  dt1=new DataTable("person1" );
    System.Data.Odbc.OdbcDataAdapter dba=new OdbcDataAdapter() ;
    dba.SelectCommand =cmd;
    dba.Fill(ds1,0,10,"person1");这里用的是ODBC连接
      

  7.   

    看来我要重修中文了,我想知道的是怎样绑定DATAGRID,怎么你们就是不明白呢?string strConnection="server=baoan;database=web;user id=sa;password=;";
    SqlConnection thisConnection=new SqlConnection(strConnection);
    SqlCommand thisCommand=new SqlCommand("select * from AAUser",thisConnection);  
    DataSet ds = new DataSet();
    thisConnection.Open();
    SqlDataAdapter dadp=new SqlDataAdapter(thisCommand);
    dadp.Fill(ds);
    dgd.DataSource=ds.Tables(0);
    thisConnection.Close();
    这样错吗?