Showing posts with label tour world. Show all posts
Showing posts with label tour world. Show all posts

Tuesday, June 12, 2007

Sql 2000 procedure

 摘要:本文以SQL Server2000存储过程为例,探讨了存储过程的优点,并以Visual Basic6.0程序设计语言给出了调用存储过程的关键代码。

  关键字:数据库;SQL Server2000;存储过程;应用程序;VB
 
  存储过程

  存储过程是存储在服务器上的一组预编译的Transact-SQL语句,是一种封装重复任务操作的方法,支持用户提供的变量,具有强大的编程功能。它类似于DOS系统中的BAT文件。在BAT文件中,可以包含一组经常执行的命令,这组命令通过BAT文件的执行而被执行。同样的道理,可以把要完成某项任务的许多Transact-SQL语句写在一起,组织成存储过程的形式,通过执行该存储过程就可以完成这项任务。存储过程与BAT文件又有差别,即存储过程已经进行了预编译。

  1、创建存储过程的方法

  在Transact-SQL语言中,创建存储过程可以使用CREATE PROCEDURE语句,其语法形式如下:






CREATE PROC[EDURE] procedure_name[;number]
[{@parameter data_type}[VARYING][=default][OUTPUT]
]],…n]
[WITH{RECOMPILE|ENCRYPTION|RECOMPILE,ENCRYPTION}]
[FOR REPLICATION]
AS sql_statement[…n]

  在上面的CREATE PROCEDURE语句中,方括号"[ ]"中的内容是可选的,花括号"{}"中的内容是必须出现的,不能省略,[,…n]表示前面的参数样式,可以重复出现。竖线"|"表示两边的选项可以任选一个。

  下面分析该语句中各种选项的含义。

  CREATE PROCEDURE是关键字,也可以写成CREATE PROC。

  procedure_name是该存储过程的名称,名称可以是任何符合命名规则的标示符。名称后的[;number]参数表示可以定义一系列的存储过程名称,这些存储过程的数量由number指定。

  参数名称可以使用@parameter data_type来指定。在Transact-SQL语言中,用户定义的参数名称前面加"@"符号,这些数据类型是Transact-SQL语言允许的各种数据类型,包括系统提供的数据类型和用户定义的数据类型。

  当参数类型为cursor时,必须使用关键字VARYING和OUTPUT。VARYING表示结果集可以是一个输出参数,其内容是动态的。该关键字只能在使用游标作为数据类型时使用。关键字OUTPUT表示这是一个输出参数,可以把存储过程执行的结果信息返回应用程序。
default用于指定参数的默认值。

  RECOMPILE选项表示重新编译该存储过程。该选项只是在需要的时候才使用,例如经常需要改变数据库模式时。

  ENCRYPTION选项用来加密创建存储过程的文本,防止他人查看。

  选项FOR REPLICATION主要用于复制过程中。注意,该选项不能和选项RECOMPILE同时使用。

  AS是一个关键字,表示其后的内容是存储过程的语句。参数sql-statement[…n]表示在一个存储过程中可以包含多个Transact-SQL语句。

  2、存储过程的优点

  在频繁访问数据库的系统中,开发者都乐于使用存储过程,这与存储过程的下列优点是分不开的。

  ⑴ 存储过程可以与其他应用程序共享应用程序的逻辑,从而确保一致的数据访问和操纵。

  ⑵ 存储过程提供了一种安全机制。如果用户被授予执行存储过程权限,那么即使该用户没有访问在执行该存储过程中所参考的表或视图的权限,该用户也可以完全执行该存储过程而不受到影响。因此,可以创建存储过程来完成所有的增加、删除等操作,并且可以通过编程控制上述操作中对信息的访问权限。

  ⑶ 存储过程执行速度快,便于提高系统的性能。由于存储过程在第一次执行之后,其执行规划就驻存在过程高速缓冲存储区中,在以后的操作中,只需从过程高速缓冲存储区中调用编译好的二进制形式存储过程来执行。

  ⑷ 使用存储过程可以减少网络传输时间。如果有一千条Transact-SQL语句的命令,一条一条地通过网络在客户机和服务器之间传送,那么这种传输所耗费的时间将很长。但是,如果把这一千条Transact-SQL语句的命令写成一条较为复杂的存储过程命令,这时在客户机和服务器之间网络传输所需的时间就会大大减少。

  SQL Server 2000数据库存储过程的调用

  VB作为当今应用极为普遍的数据库客户端开发工具之一,对客户端应用程序调用服务器端存储过程提供了强大的支持。特别是随着VB6.0的推出,VB客户端应用程序可以方便地利用ADO的对象和集合来实现对数据库存储过程的调用。

  在笔者编写的科技档案管理系统中,就是采用VB作为开发平台,采用SQL Server2000数据库管理数据,在这个科技档案管理系统中有海量的数据,并且对数据库有频繁的访问,利用存储过程访问数据库节省了执行时间,大大提高了系统的性能。

  1、ADO简介

  ADO控件(也称为ADO Data控件)与VB固有的Data控件相似。使用ADO Data控件,可以利用Microsoft ActiveX Data Objects(ADO)快速建立数据库绑定控件和数据提供者之间的连接。

  ADO Data控件可以实现以下功能:

  ·连接一个本地数据库或远程数据库。

  ·打开一个指定的数据库表,或定义一个基于结构化查询语言(SQL)的查询、存储过程或该数据库中的表的视图的记录集合。

  ·将数据字段的数值传递给数据绑定控件,可以在这些控件中显示或更改这些数值。

  ·添加新的记录,或根据更改显示在绑定的控件中的数据来更新一个数据库。

  2、数据库的连接

  数据库的连接可通过ADO控件实现,为此,必须在工程部件中选择Microsoft ADO Data Control 6.0 (OLEDB),然后在窗体中添加ADO控件。利用ADO连接数据库有两种方法,具体如下。

  1) 通过ADODC属性页实现连接

  在ADODC属性页中选择生成按钮,进入数据链接属性对话框;然后选择该对话框中的连接属性页,选择或输入服务器名称和数据库等重要信息;最后测试连接,连接成功后,按确定按钮,返回到属性页对话框,可获得连接字符串,如下例:






Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Science_File;Data Source=Data_Server

  其中sa是用户名;Science_File是数据库名;Data_Server是数据库名。

  通过下列语句,即可连接到指定的数据库:





dim odbcstr as String, adocon As New ADODB.Connection
odbcstr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Science_File;Data Source=Data_Server"
adocon.Open odbcstr '连接到数据库

  2) 直接使用连接语句实现

  连接数据库的语句如下:






Dim ado as ADODC
ado.ConnectionString = "Provider=SQLOLEDB.1;Password=" & User_Pwd & ";Persist Security Info=True;User ID=" & User_Name & ";Initial Catalog=" & Data_Name & ";Data Source=" & server_name

  其中User-Pwd是用户密码;User_Name是用户名;Data_Name是数据库名;server_name是服务器名。

  连接数据库成功后就可以调用存储过程执行操作。

  3、存储过程的调用

  假设有一个名为doc_ProcName存储过程,该存储过程有一个输入参数,一个输出参数。

  1) 直接传递参数调用存储过程

  直接传递参数方法主要通过以下几个步骤来实现:

  (1) 通过ADODB的Connection对象打开与数据源的连接;

  (2) 通过ActiveConnection指定Command对象当前所属的Connection对象;

  (3) 通过CommandText属性设置Command对象的源,即要调用的存储过程;

  (4) 通过CommandType属性确定Command对象的源类型,如果源类型为存储过程CommandType即为adCmdStoredProc;

  (5) 通过Command对象的Parameters集合向所调用的存储过程传递参数,其中对象Parameters(0)为执行存储过程的返回值,返回值为0则执行存储过程成功;

  (6) 通过Eexecute方法执行在 CommandText 属性中指定的存储过程。

  以存储过程doc_ProcName为例,关键代码如下:






Dim strS As String '定义一变量
Dim adoconn As New ADODB.Connection 'Connection 对象代表了打开与数据源的连接。
Dim adocomm As New ADODB.Command 'Command 对象定义了将对数据源执行的指定命令。
Dim ReturnValue As Integer '调用存储过程的返回值
adoconn.ConnectionString = Adodc1.ConnectionString 'Adodc1为窗体中的ADO控件,并已成功连接数据库
adoconn.Open
Set adocomm.ActiveConnection = adoconn '指示指定的 Command对象当前所属的 Connection对象。
adocomm.CommandText = "doc_ProcName" '设置Command对象源。
adocomm.CommandType = adCmdStoredProc '通知提供者CommandText属性有什么,它可能包括Command对象的源类型。设置这个属性优化了该命令的执行。
adocomm.Parameters(1) = "1"
adocomm.Parameters(2) = "OutputParameters" 'OutputParameters可以为任意的字符串或数字
adocomm.Execute
ReturnValue = adocomm.Parameters(0) '存储过程的返回值,返回0则成功执行。
strS = adocomm.Parameters(2) '把存储过程的输出参数的值赋给变量strS

  2) 追加参数法调用存储过程

  追加参数通过CreateParameter方法,用来指定属性创建新的Parameter对象。具体语法如下:






Set parameter = command.CreateParameter (Name, Type, Direction, Size, Value)

  ·Name 可选,字符串,代表 Parameter 对象名称。
  ·Type 可选,长整型值,指定 Parameter 对象数据类型。
  ·Direction 可选,长整型值,指定 Parameter 对象类型。
  ·Size 可选,长整型值,指定参数值最大长度(以字符或字节数为单位)。
  ·Value 可选,变体型,指定 Parameter 对象值。

  这种方法与上面一种方法的分别主要在于,追加参数的方法在向存储过程传递参数时,这种方法首先通过CreateParameter方法为存储过程创建参数,然后通过Append方法将创建的参数追加到Parameters集合中去。

  仍然以存储过程doc_ProcName的调用为例,关键代码如下:





Dim mRst As ADODB.Recordset 'Recordset 对象表示的是来自基本表或命令执行结果的记录全集。
Dim prm As ADODB.Parameter 'Parameter 对象代表参数或与基于参数化查询或存储过程的Command 对象相关联的参数。
adoconn.ConnectionString = Adodc1.ConnectionString
adoconn.Open
Set adocomm.ActiveConnection = adoconn
adocomm.CommandText = "doc_ProcName"
adocomm.CommandType = adCmdStoredProc
Set prm = adocomm.CreateParameter("parameter1", adTinyInt, adParamInput, , "1")
adocomm.Parameters.Append prm
Set prm = adocomm.CreateParameter("parameter2", adInteger, adParamOutput)
adocomm.Parameters.Append prm
Set mRst = adocomm.Execute
ReturnValue = adocomm.Parameters(0)

  以上代码中未定义的变量以及未注释的语句与前述相同。

  结束语

  在应用程序中调用服务器端存储过程,不仅能显著提高整个应用的性能,而且能加强对数据库数据的保护。VB为客户端应用程序调用存储过程提供了一组方便而有效的方法。


Thursday, June 7, 2007

Enjoy the life in Manila (caylabney)

For so long , i have been here for nearly One year! In one word , here We learned different things here and got lots of friends here . Though you ask me to leave without say goodbye , i can not do it. As the guys at the same age , i lost the focus on the life , the same as enid, but she did it well.Now it is my turn to show good in the life. Never lost confidence. I think nearly 1 month later , i will be in Shenzhen, and start another page of life , hope it will be a fruitful future. Hehe , enid have talking the changes or not in her blog, and my best friend also referred it on the day i met him, then from that moment , the life changed totally , it brought me into the trouble .i think it is the timing to change myself and life. So it is tough for me to watch much more clear about the society and the life around you ,though the people around you . Always i hope that i can go out with the friends i knew here , coz i know days later we will just can contact each other by email and Yahoo messager only . That's why we treasure the happy hours together . and totally , during these days in manila , the officemates and the friends are so nice .But it fadeaway our enthusiasm .I hope the new guys from china will enjoy the life here for a whole year. And also hope our friends here can alw ays enjoy the happy hours no matter in the office also in the family. We won't forget wat you brought us before. The experience will always in mind , the happy screens will be as the pictures in our brain.
Remember that Gnet said when i said "thx" to them , it seems like a thx speech , hehe , yep! may be , it like a speech, but it from my heart. It only 1month nearly. S o soonly we will miss each other.Though we may meet again ,but it will be a long time .Thanks again! to office mates to our friends.I m going to have a summary about the life living here in manila.hehe .....i think i will be the one to record the latest days in manila.heheh ...take a good view of the city again.Bring back the memory of it and share the experience with my friends.Though there's some bad guys in manila, though there's bad guys anywhere in the world. But the people here a very kindly and friendly mostly.
And wat i can not forget is that the good view of the beach and the holiday halidome . We just have been several place for holiday but the memory here in that place is very nice......hehe ..... it seems we like the traveller here in Philippines , but at least we learned English /Learned working experience here and started our life it is totally different for a fresh man to have this kinda of working experience.and i will shows out the pictures here in the Manila. The summary will be left to the next post. here we go............

the farmer working in the farm, hehe ....it brought me back to my childhood.....in the old days we have the same work.And i used to enjoy the feeling working in the farm,hehehehe , as a girl said"we can not bring back the old days"



the sky and the fields are connected together , it looks like a picture ,and it is the natural one in our eyes. Always stay in the city you can not imagine wat a nice view it shows .So don't bury yourself in the damn city ,go out and try to use the different sight to enjoy the nature.you will have the totally different feeling.
this is the first sight of the cablabney ,so near though so far away we still have to go across the hill way to down to the beach. But it is so excited that we are near our first trip destination.and going to enjoy our first office mates outing.!!!!!!!!HUHU..........

Try to see it in another angle ,see it is different at all. the mountain looks like a girl with the green skirt and always with energy , and powerful to welcome the customers coming. Waving her hands and shaking her long green hair......
The Sky is blue and the sea is blue , and the sun is light and our heart is bright too.step into the totel from the door, hey .. "huhuhuh!!!!"

The frist time to the beach hotel , the decoration of the hotel is so classical and make the traveller feel warm and quiety , so nice and so cool.

you will feel it is so nice , wat a kind of seesight that the god gave it as a gift. So we live in the world and the beautiful sight is besides us, don't miss the chance to enjoy it.... so i use the "Love the world"as the subject of this blog , just becoz i love this kind of view in philippines, that's why i wanna travel around the world and find the good views ..hehe ... hope i walk around the whole world.Don't just saying it , work on it ,,,,,make it to true...........


the light is a little dark , but it is also nice.
a ship and travel with it , around the sea of the world, wat a kind of feeling , during the life if i have the chance to get a ship i will enjoy it in the sea..Can not waiting to have that kind of life.
this is the picture i like mostly, heheh... coz i make me to see the feet of the mountain and the start of the sea... that says the start and the end always can be a circle in the world. one start it means another one is being generated never end never disappeared , it just show out in different type and status.
Hehehehe , the trees it make me remember that , during the middle school that i just watch that the discovery chanel and that , i feel that the trees also the plants have the same feeling of information as human, and days later it is improved in the discovery chanel , i was so excited. from then on i feel that life always show amazing to human.
green mountain , i like it , don't you ?!??!?!
a piere that , we can imagine that everyone have his own pier , when where how it is just the problem of timing . when time comes that you will load in your own pier, Kuya got into his pier , hope he will happy forever , sure , he will, hahhahah
Houses water ,trees , hehehehe, got relax and enjoy the air in the cablaybney ,hahahah ......i really lost myself in that kind of life , but the time is too short ,just 2days....no less than it , but we start our first job by that time....

Sunday, May 20, 2007

something i just wanna release

During the trip From HK to shenzhen

it is a good experience and something should leave to think about .In a book , life is to get himself out of the tough world. But if you are full of appetite ,so you can not just release yourself, you have to follow your true thoughts not only the ideas of others . Now after the stories happened in the Guangzhou ,i totally found that i lost my 6th feeling , and feel like be foolish. Because it seems that i got my sense sleeping and avoid of thinking . So it makes me can not distingush the good or bad though trick. What should i do . release myself from the jail? . but the jail is made by myself. Thoughts Jail. Good way , I wanna get back my 6th sense.... .. Now my 6th is just the moment when i made decision.But before it will just show before the events comes out ....... How should i make it much earlier .. that means .. the feeling of the sense should before much more earlier. During the raining day , it will be dark for me , So someone called it bad luck . Exactly it true for me .So i will not make any big decision during the dark days. God give it back to me . I will really preciated it .