如何以Windows窗体显示数据库中的数据 [英] how to display the data from the database in windows form
问题描述
我是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屋!