本文介绍了从select statment中检索结果到dataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
App.config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="DBCS" connectionString="data source=.;Database=MnStores; Integrated Security = True"/>
</connectionStrings>
</configuration>
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MnStores
{
public partial class Form1 : Form
{
public Form1()
{ InitializeComponent();
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
string command = "select * from tbl_CategoryProducts";
SqlCommand cmd = new SqlCommand(command, con);
cmd.Connection = con;
con.Open();
dataGridView1.DataSource = cmd.ExecuteReader();
con.Close();
}
}
推荐答案
DataTable _dt = new DataTable();
using (SqlConnection sqlcon = new SqlConnection(ConStr))
{
string sqlstr = "select * from tbl_CategoryProducts";
using (SqlDataAdapter adap = new SqlDataAdapter(sqlstr, sqlcon))
{
adap.Fill(_dt);
cmbProvider.DataSource = _dt;
}
}
AVK
AVK
这篇关于从select statment中检索结果到dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!