如何将数据集绑定到数据表 [英] how to bind data set into data table
本文介绍了如何将数据集绑定到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string Constr = ConfigurationManager.ConnectionStrings["YET_DatabaseConnectionString"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(Constr);
conn.Open();
string DConsignor = DDConsigner.Text;
sql = "select * from Bilty where Consigner=''"+ DConsignor +"''";
SqlCommand cmd = new SqlCommand(sql, conn);
// SqlDataReader rd = cmd.ExecuteReader();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
// dt= this.getgridforConsignor(DConsignor);
getgridforConsignor(DConsignor);
//GridView1.DataSource = dt;
//GridView1.DataBind();
conn.Close();
推荐答案
使用DataAdapter填充数据集
http://msdn.microsoft.com/en-us/library/bh8kx08z%28v = VS.100%29.aspx [ ^ ]
例如
Use a DataAdapter to fill the dataset
http://msdn.microsoft.com/en-us/library/bh8kx08z%28v=VS.100%29.aspx[^]
e.g.
// Assumes that connection is a valid SqlConnection object.
string queryString =
"SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");
数据集将包含表,您可以获取表的引用,然后对表进行数据绑定
The DataSet will contain tables, which you can obtain a reference to and then DataBind to your grid
string strConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection strConn = new SqlConnection(strConnectionString );
strConn.Open();
string sqlStatement = "SELECT * from tblCountry";
SqlCommand cmd = new SqlCommand(sqlStatement, strConn);
cmd.CommandType = CommandType.Text;
DataSet ds = new DataSet();
SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
sqlDA.Fill(ds);
cmd.ExecuteNonQuery();
if (ds.Tables[0].Rows.Count > 0)
{
grdEditCountry.DataSource = ds.Tables[0];
grdEditCountry.DataBind();
}
strConn.Close();
这篇关于如何将数据集绑定到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文