BETA2中操作SQL数据库
来源:互联网 发布:百电通软件下载 编辑:程序博客网 时间:2024/04/29 23:28
<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>
首先我们要知道,在ADO。NET环境下,我们是通过System.Data.SQLClient这个名字空间来操作的,另外还有一个System.Data.SQLTypes名字空间描述了SQL SERVER的字段类型,但它并不直接参与数据库的操作,所以我们重点放在System.Data.SQLClient的使用上!
按照惯例,我还是先列出我的例程:这个程序是我在写一个用户注册系统时用到的,先看程序:
using System;
using System.Data;
using System.Data.SQLClient;
//通过用户ID号,取得用户资料
public UserInfo GetUserInfo(string id)
{
SQLConnection myconn=SohoTool.SetConn(); //看备注一 myconn.Open();
//设置SQL查询语句
string selectstr="select * from userinfo where id=" + id ;
try{
SQLCommand mycmd = new SQLCommand(selectstr,myconn);
SQLDataReader mySQLreader = mycmd.ExecuteReader();
if(mySQLreader.Read()) //假如存在该用户,则录入需要的属性!
{
this.i_id=(int)mySQLreader["id"];
this.s_name=mySQLreader["name"].ToString();
this.s_nicheng=mySQLreader["nicheng"].ToString();
this.s_email=mySQLreader["email"].ToString();
this.s_password=mySQLreader["password"].ToString();
this.s_http=mySQLreader["http"].ToString();
this.s_oicq=mySQLreader["oicq"].ToString();
this.s_tag=mySQLreader["tag"].ToString();
this.i_charm=(int)mySQLreader["charm"];
this.i_score=(int)mySQLreader["score"];
this.i_bbswords=(int)mySQLreader["bbswords"];
this.s_pic=mySQLreader["pic"].ToString();
this.d_regtime=(DateTime)mySQLreader["regtime"];
}
else
{
throw(new Exception("没有该用户!" ));
}
}
catch(Exception e)
{
throw(new Exception("数据库操作发生错误!" + e.Message));
}
myconn.Close();
return(this);
}
上面的程序实现的是:通过用户的ID号来取得用户个人资料的,下面我们看看程序中有什么新的东西!
首先我们开程序开始处引入了名字空间System.Data.SQLClient;这样我们就可以用它来操作SQL数据库了!
1. 在函数开始我们通过SQLConnection myconn=SohoTool.SetConn(); 得到SQL数据库的连接。
2.设置了SQL查询语句后,我们又定义了一个SQLCommand对象,并实例化,其实在ADO。NET中大部分SQL语句都能很方便的用Command来执行,但是如果和DateSet结合的话,就要引入其它的一些东西了,这个到后面我们在说!
3.定义SQLDataReader对象,并通过SQLCommand对象执行SQ语句,然后 将结果存入 SQLDataReader对象中,语句如下:SQLDataReader mySQLreader = mycmd.ExecuteReader();
4.设置属性,并返回需要的结果
看了我前面文章的人,应该都能感觉到,其实操作SQL数据库原来和操作ACCESS数据库并没有什么区别嘛,无非是名字空间、COMMAND、READER的写法变了个样呀!呵呵,实际情况也几乎就是这样的!
备注一、上面程序中我得到SQL数据库连接是用的我自己的类,下面我吧这个程序也写出来!
namespace Soholife
{
using System;
using System.Data;
using System.Data.SQLClient;
public class SohoTool
{
//建立与SQL数据库的连接
public static SQLConnection SetConn()
{
string connstr="server=soho;database=soholife;uid=sa;pwd=;";
SQLConnection tempconn= new SQLConnection(connstr);
return(tempconn);
}
}
}
这个程序我想不需要什么解释的吧,用处就是得到数据库连接,有些个人体会是, BETA1下,我们可以通过继承CONNECTION,来建立自己的CONNECTION类,而在BETA2下,却无法这样做了,不知道为什么要把CONNECTION类作成密封的形式!
好了,这次就先写到这里,下一次,我把会说说如何通过SQLCommand对象执行UPDATE、INSERT、DELETE语句!如果朋友们有问题也可以给我写信讨论!
BETA2中操作SQL数据库';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>
在前面,我已经说了如何在BETA2的环境下,操作ACCESS数据库,这次我们来看看如何通过ADO。NET来操作SQL SERVER数据库!首先我们要知道,在ADO。NET环境下,我们是通过System.Data.SQLClient这个名字空间来操作的,另外还有一个System.Data.SQLTypes名字空间描述了SQL SERVER的字段类型,但它并不直接参与数据库的操作,所以我们重点放在System.Data.SQLClient的使用上!
按照惯例,我还是先列出我的例程:这个程序是我在写一个用户注册系统时用到的,先看程序:
using System;
using System.Data;
using System.Data.SQLClient;
//通过用户ID号,取得用户资料
public UserInfo GetUserInfo(string id)
{
SQLConnection myconn=SohoTool.SetConn(); //看备注一 myconn.Open();
//设置SQL查询语句
string selectstr="select * from userinfo where id=" + id ;
try{
SQLCommand mycmd = new SQLCommand(selectstr,myconn);
SQLDataReader mySQLreader = mycmd.ExecuteReader();
if(mySQLreader.Read()) //假如存在该用户,则录入需要的属性!
{
this.i_id=(int)mySQLreader["id"];
this.s_name=mySQLreader["name"].ToString();
this.s_nicheng=mySQLreader["nicheng"].ToString();
this.s_email=mySQLreader["email"].ToString();
this.s_password=mySQLreader["password"].ToString();
this.s_http=mySQLreader["http"].ToString();
this.s_oicq=mySQLreader["oicq"].ToString();
this.s_tag=mySQLreader["tag"].ToString();
this.i_charm=(int)mySQLreader["charm"];
this.i_score=(int)mySQLreader["score"];
this.i_bbswords=(int)mySQLreader["bbswords"];
this.s_pic=mySQLreader["pic"].ToString();
this.d_regtime=(DateTime)mySQLreader["regtime"];
}
else
{
throw(new Exception("没有该用户!" ));
}
}
catch(Exception e)
{
throw(new Exception("数据库操作发生错误!" + e.Message));
}
myconn.Close();
return(this);
}
上面的程序实现的是:通过用户的ID号来取得用户个人资料的,下面我们看看程序中有什么新的东西!
首先我们开程序开始处引入了名字空间System.Data.SQLClient;这样我们就可以用它来操作SQL数据库了!
1. 在函数开始我们通过SQLConnection myconn=SohoTool.SetConn(); 得到SQL数据库的连接。
2.设置了SQL查询语句后,我们又定义了一个SQLCommand对象,并实例化,其实在ADO。NET中大部分SQL语句都能很方便的用Command来执行,但是如果和DateSet结合的话,就要引入其它的一些东西了,这个到后面我们在说!
3.定义SQLDataReader对象,并通过SQLCommand对象执行SQ语句,然后 将结果存入 SQLDataReader对象中,语句如下:SQLDataReader mySQLreader = mycmd.ExecuteReader();
4.设置属性,并返回需要的结果
看了我前面文章的人,应该都能感觉到,其实操作SQL数据库原来和操作ACCESS数据库并没有什么区别嘛,无非是名字空间、COMMAND、READER的写法变了个样呀!呵呵,实际情况也几乎就是这样的!
备注一、上面程序中我得到SQL数据库连接是用的我自己的类,下面我吧这个程序也写出来!
namespace Soholife
{
using System;
using System.Data;
using System.Data.SQLClient;
public class SohoTool
{
//建立与SQL数据库的连接
public static SQLConnection SetConn()
{
string connstr="server=soho;database=soholife;uid=sa;pwd=;";
SQLConnection tempconn= new SQLConnection(connstr);
return(tempconn);
}
}
}
这个程序我想不需要什么解释的吧,用处就是得到数据库连接,有些个人体会是, BETA1下,我们可以通过继承CONNECTION,来建立自己的CONNECTION类,而在BETA2下,却无法这样做了,不知道为什么要把CONNECTION类作成密封的形式!
好了,这次就先写到这里,下一次,我把会说说如何通过SQLCommand对象执行UPDATE、INSERT、DELETE语句!如果朋友们有问题也可以给我写信讨论!
<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>
- BETA2中操作SQL数据库
- 贴篇文章,BETA2中ACCESS操作数据库
- C# BETA2中操作ACCESS数据库(三)
- 第二部分,BETA2操作ACCESS数据库
- 第三部分,BETA2操作ACCESS数据库
- 在2005Beta2中编辑Transact-SQL代码
- ACCESS中操作 SQL Server 数据库
- sql server 中跨数据库操作
- VC6.0中操作SQL Server数据库
- VC++中采用ADO操作SQL数据库
- ASP.NET中操作SQL数据库
- MFC中操作SQL SERVER数据库
- VC++中采用ADO操作SQL数据库
- Android中sql数据库adb操作
- Unity中对SQL数据库的操作
- SQL中数据库的基本操作
- SQL中数据库的基本操作
- MySQL中操作关系型数据库 SQL
- VB.Net问答全集(推荐)
- 创建一个Windows Service应用程序
- 转贴:Microsoft Application Center 2000 组件负载平衡技术概述(1)
- 在两个ASP.NET页面之间传递值
- c#中的接口
- BETA2中操作SQL数据库
- 在.NET中使用API的方法
- 漫谈.Net中的自动垃圾收集(Garbage Collection)机制(转)
- 用ASP.NET构建完整E-mail发送系统
- 轻松调整VB.NET控件
- 贴篇文章,BETA2中ACCESS操作数据库
- To CNET:全局热键的例子,不知道有没有用
- 除了使用hook外,下面的代码也能够在全局捕捉鼠标:(csdn)
- Visual Basic .NET 已不再支持的 Visual Basic 6.0 功能