ADO.NET中DataSet的使用
2012-10-10 15:55
323 查看
1.读取:
在web.config添加:
<connectionStrings>
<add name="carConnectionString" connectionString="Data Source=JY-LANBJ;Initial Catalog=car;Persist Security Info=True;User ID=root;Password=root" providerName="System.Data.SqlClient"/>
</connectionStrings>
在**.aspx.cs页面的Page_Load方法中添加:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["carConnectionString"].ToString());
{
string cmd1 = "select * from carworkers";
SqlDataAdapter da = new SqlDataAdapter(cmd1, con);
DataSet ds = new DataSet();//创建数据集
da.Fill(ds);//填充数据集
DataTable dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count;i++ )
{
Response.Write(dt.Rows[i]["power"].ToString() + "<BR>");
}
}
注意先添加以下命名空间:
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
---------------------------------------------------------------------------------------------------------------------------------
2.插入:
在**.aspx.cs页面的Page_Load方法中添加:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["carConnectionString"].ToString());
{
string cmd1 = "select * from carworkers";
SqlDataAdapter da = new SqlDataAdapter(cmd1, con);
SqlCommandBuilder cb = new SqlCommandBuilder(da); //为Adapter创建builder实例,才可以进行插入、修改、更新
DataSet ds = new DataSet();//创建数据集
da.Fill(ds);//填充数据集
DataTable dt = ds.Tables[0];
DataRow newRow = dt.NewRow();
newRow["username"] = "Jolin";
newRow["power"] = 0;
newRow["id"] = 99;
dt.Rows.Add(newRow);
da.Update(ds);
for (int i = 0; i < dt.Rows.Count;i++ )
{
Response.Write(dt.Rows[i]["power"].ToString() + "<BR>");
}
}
---------------------------------------------------------------------------------------------------------------------------------
3.删除
在**.aspx.cs页面的Page_Load方法中添加:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["carConnectionString"].ToString());
{
string cmd1 = "select * from works where wid='" + did+"'";
SqlDataAdapter da = new SqlDataAdapter(cmd1, con);
SqlCommandBuilder cb = new SqlCommandBuilder(da); //为Adapter创建builder实例,才可以进行插入、修改、更新
con.Open();
DataSet dss = new DataSet();//创建数据集
da.Fill(dss);//填充数据集
DataTable dt = dss.Tables[0];
dt.Rows[0].Delete();
da.Update(dss);
con.Close();
Response.Write("<script LANGUAGE='javascript'>location.href ='default.aspx?username="+username+"';</script>");
}
在web.config添加:
<connectionStrings>
<add name="carConnectionString" connectionString="Data Source=JY-LANBJ;Initial Catalog=car;Persist Security Info=True;User ID=root;Password=root" providerName="System.Data.SqlClient"/>
</connectionStrings>
在**.aspx.cs页面的Page_Load方法中添加:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["carConnectionString"].ToString());
{
string cmd1 = "select * from carworkers";
SqlDataAdapter da = new SqlDataAdapter(cmd1, con);
DataSet ds = new DataSet();//创建数据集
da.Fill(ds);//填充数据集
DataTable dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count;i++ )
{
Response.Write(dt.Rows[i]["power"].ToString() + "<BR>");
}
}
注意先添加以下命名空间:
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
---------------------------------------------------------------------------------------------------------------------------------
2.插入:
在**.aspx.cs页面的Page_Load方法中添加:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["carConnectionString"].ToString());
{
string cmd1 = "select * from carworkers";
SqlDataAdapter da = new SqlDataAdapter(cmd1, con);
SqlCommandBuilder cb = new SqlCommandBuilder(da); //为Adapter创建builder实例,才可以进行插入、修改、更新
DataSet ds = new DataSet();//创建数据集
da.Fill(ds);//填充数据集
DataTable dt = ds.Tables[0];
DataRow newRow = dt.NewRow();
newRow["username"] = "Jolin";
newRow["power"] = 0;
newRow["id"] = 99;
dt.Rows.Add(newRow);
da.Update(ds);
for (int i = 0; i < dt.Rows.Count;i++ )
{
Response.Write(dt.Rows[i]["power"].ToString() + "<BR>");
}
}
---------------------------------------------------------------------------------------------------------------------------------
3.删除
在**.aspx.cs页面的Page_Load方法中添加:
SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["carConnectionString"].ToString());
{
string cmd1 = "select * from works where wid='" + did+"'";
SqlDataAdapter da = new SqlDataAdapter(cmd1, con);
SqlCommandBuilder cb = new SqlCommandBuilder(da); //为Adapter创建builder实例,才可以进行插入、修改、更新
con.Open();
DataSet dss = new DataSet();//创建数据集
da.Fill(dss);//填充数据集
DataTable dt = dss.Tables[0];
dt.Rows[0].Delete();
da.Update(dss);
con.Close();
Response.Write("<script LANGUAGE='javascript'>location.href ='default.aspx?username="+username+"';</script>");
}
相关文章推荐
- ADO.NET 数据库实例教程-DataSet的使用
- C#ADO.NET使用Dataset的步骤
- ado.net中的DataSet使用
- ADO.NET笔记——使用DataSet返回数据
- 使用ADO.NET2.0提升数据交互性能 DataSet 数据表
- 学习笔记》ADO.NET》在ADO.NET中使用DataSet
- ADO.NET_包括DataReader和dataSet的使用
- 【ADO.NET】DataSet使用方法
- ADO.NET 更新数据库的2种方式::(1)使用DataSet对象更新数据集;(2)使用SqlCommand对象更新单条记录
- 【ADO.NET】8、DataSet的使用
- ADO.Net之使用DataRead Or DataSet
- 编码实现>微软数据访问技术ADO.NET>使用DataSet>
- 如何使用ADO.net
- ADO.NET的DataSet和ADO的Recordset的比较
- 使用ADO.NET2.0提升数据交互性能(4)
- 34:ASP.NET复习系列- ADO.NET提高-深入了解DataSet
- c# ado.net DataSet用法
- .NET数据库编程求索之路--5.使用ADO.NET实现(三层架构篇-使用List传递数据)(3)
- 数据访问:使用 ADO.NET 的最佳实践
- ADO.net如何对没有DataAdapter的DataSet的更新