sql数据库已连接但未显示datagridview [英] sql database connected but not showed the datagridview
本文介绍了sql数据库已连接但未显示datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我这样编码,但datagridview没有显示数据库。请给我一个很好的解决方案。
Hello
I have coded like that but the datagridview is not showing the database. Kindly provide me good solution.
public partial class frmMaterial : Form
{
public frmMaterial()
{
InitializeComponent();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";
connection.Open();
SqlCommand command = new SqlCommand("select * from mat_utiAndtrans", connection);
DataTable ds = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
cmbcode.DataSource = ds;
da.Fill(ds);
cmbcode.DisplayMember = "Code";
cmbcode.ValueMember = "Code";
}
private void btnsearch_Click(object sender, EventArgs e)
{
if (cmbcode.SelectedIndex >= 0)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";
connection.Open();
DataSet dl = new DataSet();
string a = cmbcode.SelectedValue.ToString();
SqlCommand com = new SqlCommand("select * from mat_utiAndtrans where Code='" + a + "'", connection);
DataSet dts = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(com);
dataGridView1.DataSource = dts;
da.Fill(dts);
dataGridView1.DataMember = dts.Tables[0].ToString();
}
推荐答案
public partial class frmMaterial : Form
{
public frmMaterial()
{
InitializeComponent();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";
connection.Open();
SqlCommand command = new SqlCommand("select * from mat_utiAndtrans", connection);
DataTable ds = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
cmbcode.DataSource = ds;
da.Fill(ds);
cmbcode.DisplayMember = "Code";
cmbcode.ValueMember = "Code";
}
private void btnsearch_Click(object sender, EventArgs e)
{
if (cmbcode.SelectedIndex >= 0)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";
connection.Open()
string a = cmbcode.SelectedValue.ToString();
SqlCommand com = new SqlCommand("select * from mat_utiAndtrans where Code='" + a + "'", connection);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dts);
dataGridView1.DataSource = dt;
}
在frmMaterial方法中改变这样的方式
Change like this in frmMaterial method
cmbcode.DisplayMember = "Code";
cmbcode.ValueMember = "Code";
da.Fill(ds); // Fill the data first and then assign it to a control
cmbcode.DataSource = ds;
搜索按钮点击事件
in Search button click event
da.Fill(dts);
dataGridView1.DataSource = dts;
//dataGridView1.DataMember = dts.Tables[0].ToString();
在SqlDataAdapter对象da中使用SqlConnection字符串对象连接并在字符串对象中存储查询,如下所示
use SqlConnection string object connection in SqlDataAdapter object da and store query in string object as follows
string com=sqlQuery;
SqlDataAdapter da= new SqlDataAdapter(com,connection);
这篇关于sql数据库已连接但未显示datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文