private void Page_Load(object sender, System.EventArgs e)
{
string mys="select * from admin";
            string myc="server=localhost;uid=browseuser;pwd=wlhty;database=aaaa";

System.Data.SqlClient.SqlConnection myconn=new System.Data.SqlClient.SqlConnection(myc);
System.Data.SqlClient.SqlCommand mycomm=new System.Data.SqlClient.SqlCommand(mys,myconn);
myconn.Open();
System.Data.SqlClient.SqlDataReader tr=mycomm.ExecuteReader();
show.DataSource=tr;
show.DataBind();
tr.Close();
myconn.Close();
// Put user code to initialize the page here
}

解决方案 »

  1.   

    由于你问的问题的答案太多了,所以我只能这么告诉你了:在[中国青年出版社]的《C#与.NET技术平台实战演练》中的第22章有这方面的详细说明,按那上的例子做一遍就会了。
      

  2.   

    感谢您使用微软产品。在C#中,如果DataGrid绑定到单一的数据表,可以使用OleDbCommandBuilder (SqlCommandBuilder)类来非常方便地实现datagrid -数据库记录的增加、删除和存盘功能。通过OleDbCommandBuilder (SqlCommandBuilder)来自动生成DataAdapter的DeleteCommand, InsertCommand和UpdateCommand等命令。但是,需要绑定到DataGrid的数据表设定Primary Key(主键)。
    下面提供一个简单的示例程序,供您参考:
    private OleDbCommandBuilder FriendsCmdBuilder; //用于自动生成SQL指令
    private DataSet FriendsDataSet= new DataSet(); 
    private OleDbDataAdapter  FriendsAdapter;  
    ……
    // 通过GridBind,将数据表Friends绑定到DataGrid1
    private void GridBind()
    {
    String ConnStr="Provider=SQLOLEDB;server=SHA-RICKIE-01;uid=user;pwd=user;database=test";
    OleDbConnection myConnection= new OleDbConnection(ConnStr);
    FriendsAdapter = new OleDbDataAdapter("select * from friends",myConnection);
    /// 实例化OleDbCommandBuilder
    FriendsCmdBuilder = new OleDbCommandBuilder(FriendsAdapter);
    ///fill the dataset
    FriendsAdapter.Fill(FriendsDataSet, "Friends");
    ///set the dataset as a datasource for windows datagrid
    dataGrid1.SetDataBinding(FriendsDataSet,"Friends");
    }private void btnSave_Click(object sender, System.EventArgs e)
    {
    try
    {
    ///执行inserts,updates,deletes指令
    /// Without the OleDbCommandBuilder, this line would fail.
    FriendsAdapter.Update(FriendsDataSet,"Friends");
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }关于OleDbCommandBuilder的更详细信息,请参考MSDN:
    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDataOleDbOleDbCommandBuilderClassTopic.htm — 微软全球技术中心 VB支持中心本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
    为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
      

  3.   

    这个问题的答案到处都是,你键入DataGrid,在这个社区上搜一下,一大堆