DataSet dataSet = new DataSet();
DataTable dataTable = new DataTable("TableName");
dataSet.Tables.Add(dataTable);
dataTable.Columns.Add("ColumnName", typeof(int));
DataRow row = dataTable.NewRow();
row["ColumnName"] = 1;
dataTable.Rows.Add(row);
string connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=True";
string query = "SELECT * FROM TableName";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
adapter.Fill(dataSet, "TableName");
}
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(dataSet, "TableName");
}
foreach (DataRow row in dataSet.Tables["TableName"].Rows)
{
Console.WriteLine(row["ColumnName"]);
}
dataSet.Clear();
dataSet.WriteXml("data.xml");
dataSet.ReadXml("data.xml");
這些是一些基本的DataSet的使用技巧,可以幫助你在C# ADO.NET中更好地操作數(shù)據(jù)集。