用ado.net进行分布式程序设计更新数据库(学习笔记)
来源:互联网 发布:iphone7 usb共享网络 编辑:程序博客网 时间:2024/05/16 03:04
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
using System;
using System.Data ;
using System.Data.SqlClient ;
namespace AsterDnet.DataBind
{
/// <summary>
/// Summary description for GetData.
/// </summary>
public class TestData
{
private SqlDataAdapter da;
public TestData()
{
//
// TODO: Add constructor logic here
//
}
public DataSet GetDataSet()
{
string strConn="data source=ASTERDnet;initial catalog=Test;integrated security=SSPI;persist security info=False;user id=sa;workstation id=ASTERDnet;packet size=4096";
SqlConnection conn=new SqlConnection(strConn);
conn.Open();
DataSet ds=new DataSet();
SqlParameter workParam=new SqlParameter();
da=new SqlDataAdapter();
//Build the select Command
da.SelectCommand =new SqlCommand("select * from Education ",conn);
da.Fill(ds,"Education");
// Build the insert Command
da.InsertCommand = new SqlCommand("Insert into Education (ID, Education) VALUES (@ID, @Education)", conn);
workParam = da.InsertCommand.Parameters.Add("@ID", SqlDbType.Int);
workParam.SourceColumn = "ID";
workParam.SourceVersion = DataRowVersion.Current;
workParam = da.InsertCommand.Parameters.Add("@Education", SqlDbType.NChar, 50);
workParam.SourceVersion = DataRowVersion.Current;
workParam.SourceColumn = "Education";
// Build the update command
da.UpdateCommand = new SqlCommand("Update Education Set Education = @Education WHERE ID = @ID" , conn);
workParam = da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int);
workParam.SourceColumn = "ID";
workParam.SourceVersion = DataRowVersion.Original;
workParam = da.UpdateCommand.Parameters.Add("@Education", SqlDbType.NChar, 50);
workParam.SourceVersion = DataRowVersion.Current;
workParam.SourceColumn = "Education";
//Build the delete command
da.DeleteCommand =new SqlCommand("delete from Education where id=@ID",conn);
workParam=da.DeleteCommand.Parameters.Add("@ID",SqlDbType.Int );
workParam.SourceColumn ="ID";
workParam.SourceVersion =DataRowVersion.Original;
return ds;
}
//save the change dataset
public void SaveDate(DataSet dataSet,string tableName)
{
da.Update(dataSet,tableName);
}
}
}
例中我建立了一个Test的数据库,数据库中建立一个Education的表,表中建立了两个字段ID(int),Education(char),在rc3中通过。
感觉没有vb6.0用ado2.5中那么方便(只要设置ActiveConnection,然后update就一切搞定,现在要自己写如何更新的SqlCommand),不过程序员的控制能力要强一些了。
ado.net进行分布式程序设计更新数据库(学习笔记)';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
提供一个数据服务类,调用GetDataSet就得到需要的DataSet,然后在用户界面进行修改,最后调用SaveDate就可以把未定的更改保存到数据库。using System;
using System.Data ;
using System.Data.SqlClient ;
namespace AsterDnet.DataBind
{
/// <summary>
/// Summary description for GetData.
/// </summary>
public class TestData
{
private SqlDataAdapter da;
public TestData()
{
//
// TODO: Add constructor logic here
//
}
public DataSet GetDataSet()
{
string strConn="data source=ASTERDnet;initial catalog=Test;integrated security=SSPI;persist security info=False;user id=sa;workstation id=ASTERDnet;packet size=4096";
SqlConnection conn=new SqlConnection(strConn);
conn.Open();
DataSet ds=new DataSet();
SqlParameter workParam=new SqlParameter();
da=new SqlDataAdapter();
//Build the select Command
da.SelectCommand =new SqlCommand("select * from Education ",conn);
da.Fill(ds,"Education");
// Build the insert Command
da.InsertCommand = new SqlCommand("Insert into Education (ID, Education) VALUES (@ID, @Education)", conn);
workParam = da.InsertCommand.Parameters.Add("@ID", SqlDbType.Int);
workParam.SourceColumn = "ID";
workParam.SourceVersion = DataRowVersion.Current;
workParam = da.InsertCommand.Parameters.Add("@Education", SqlDbType.NChar, 50);
workParam.SourceVersion = DataRowVersion.Current;
workParam.SourceColumn = "Education";
// Build the update command
da.UpdateCommand = new SqlCommand("Update Education Set Education = @Education WHERE ID = @ID" , conn);
workParam = da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int);
workParam.SourceColumn = "ID";
workParam.SourceVersion = DataRowVersion.Original;
workParam = da.UpdateCommand.Parameters.Add("@Education", SqlDbType.NChar, 50);
workParam.SourceVersion = DataRowVersion.Current;
workParam.SourceColumn = "Education";
//Build the delete command
da.DeleteCommand =new SqlCommand("delete from Education where id=@ID",conn);
workParam=da.DeleteCommand.Parameters.Add("@ID",SqlDbType.Int );
workParam.SourceColumn ="ID";
workParam.SourceVersion =DataRowVersion.Original;
return ds;
}
//save the change dataset
public void SaveDate(DataSet dataSet,string tableName)
{
da.Update(dataSet,tableName);
}
}
}
例中我建立了一个Test的数据库,数据库中建立一个Education的表,表中建立了两个字段ID(int),Education(char),在rc3中通过。
感觉没有vb6.0用ado2.5中那么方便(只要设置ActiveConnection,然后update就一切搞定,现在要自己写如何更新的SqlCommand),不过程序员的控制能力要强一些了。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 用ado.net进行分布式程序设计更新数据库(学习笔记)
- .NET学习(7)ADO.NET进行数据库编程
- 数据库开发及ADO.NET学习笔记(一)
- 数据库开发及ADO.NET学习笔记(二)
- ADO.NET学习笔记
- ADO.NET学习笔记
- ADO.net学习笔记
- ado.net 学习笔记
- ADO.NET学习笔记
- ADO.NET 学习笔记
- ADO.NET学习笔记
- ADO.net学习笔记
- ado.net学习笔记
- ADO.NET学习笔记
- Ado.Net学习笔记
- ADO.NET 快速入门(五):从 DataSet 更新数据库
- ADO.NET学习笔记(一)
- ADO.NET学习笔记(二)
- ASP+全新接触(4) {转}
- 用Visual C#中轻松浏览数据库记录(转)
- pager procedure for sql server
- 如何让TextBox的黑色边框变成淡蓝色(borderstyle=FixedSingle)
- ntext搜索关键字
- 用ado.net进行分布式程序设计更新数据库(学习笔记)
- 模拟字符串处理函数 stuff 处理 Ntext 字段
- vs.net编译器里面的一个小技巧!一定要看约!
- [转]甲骨文函数大全
- winform同样存在这些问题
- DBA工作备忘录之三:rman备份,未使用catalog,控制文件
- 为ADO 程序员设计的 ADO.NET (转)
- 用SQL语句来建立跟踪的问题
- ASP.NET中的ViewState