如何以Windows窗体显示数据库中的数据 [英] how to display the data from the database in windows form

查看:142
本文介绍了如何以Windows窗体显示数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手.

我返回了以下代码,将数据添加到数据表中.

 私有  void  addbtn_Click(对象发​​件人,EventArgs e)
       {
          SqlCommand cmd;
           SqlConnection con =  SqlConnection(" );

           con.Open();
           cmd =  SqlCommand("  + glisttxt.Text +  ')",con);
           cmd.ExecuteNonQuery();

       }


它将值添加到数据库中.

但是数据没有显示在DataGrid视图中,请告诉代码以将数据显示到数据库中.

解决方案

 私有 无效 addbtn_Click(对象发​​件人,EventArgs e)
        {
            SqlCommand cmd,read1;
            SqlConnection con =  SqlConnection(" );
            con.Open();
            cmd =  SqlCommand("  + glisttxt.Text +  ')",con);
            cmd.ExecuteNonQuery();
            read1 = 新建 SqlCommand("  SqlCommand myCommand =  SqlCommand(" );
SqlDataAdapter oAdapter =  SqlDataAdapter(myCommand); // 您有一个执行命令的sqlDataAdapter 
数据集ds =  dataset();
oAdapter.Fill(ds);
myGridview.datasource = ds;
myGriview.databind(); // ,最后像这样将其绑定到UI层中的GridView  


更多参考链接:- http://www.dotnetperls.com/datagridview-tutorial [ SqlCommand cmd; SqlConnection con = SqlConnection(" ); con.Open(); cmd = SqlCommand(" ,con); SqlDataAdapter da = SqlDataAdapter(cmd); cmd.ExecuteNonQuery(); DataSet ds = DataSet(); da.Fill(ds); DataGrid1.DataSource = ds DataGrid1.DataMember = " ;


I am new to c#.

I have return the following code to add data to the Datatable.

private void addbtn_Click(object sender, EventArgs e)
       {
          SqlCommand cmd;
           SqlConnection con=new SqlConnection("Data Source=name;Initial Catalog=month;Integrated Security=True");

           con.Open();
           cmd=new SqlCommand("Insert into grossarynames values(' "+glisttxt.Text+" ')",con);
           cmd.ExecuteNonQuery();

       }


It is adding the values to the database.

But the data is not displayed in the DataGrid view please tell the code to display the data to the database.

private void addbtn_Click(object sender, EventArgs e)
        {
            SqlCommand cmd,read1;
            SqlConnection con=new SqlConnection("Data Source=name;Initial Catalog=month;Integrated Security=True");
            con.Open();
            cmd=new SqlCommand("Insert into grossarynames values(' "+glisttxt.Text+" ')",con);
            cmd.ExecuteNonQuery();
            read1=new SqlCommand("Select * from grossarynames,con);
            SqlDataReader dr=read1.executeReader();
            DataTable dt=new Datatable();
            dt.Load(dr);
            datagridview.source=dt;
        }



check code


Try

SqlCommand myCommand = new SqlCommand("your SELECT command");
SqlDataAdapter oAdapter = new SqlDataAdapter(myCommand); // You have a sqlDataAdapter which executes your command
dataset ds = new dataset();
oAdapter.Fill(ds);
myGridview.datasource = ds;
myGriview.databind();  // and then finally bind it to your GridView in your UI layer like this


More Reference Link :-
http://www.dotnetperls.com/datagridview-tutorial[^]


use below code to display the data:

SqlCommand cmd;
            SqlConnection con=new SqlConnection("Data Source=name;Initial Catalog=month;Integrated Security=True");
 
            con.Open();
            cmd=new SqlCommand("Select * from grossarynames",con);
SqlDataAdapter da = new SqlDataAdapter(cmd); 
            cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds
DataGrid1.DataMember = "grossarynames";


这篇关于如何以Windows窗体显示数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆