数据蕴含丰富的信息,数据就是资源。
不同的语言,由于各自的语法特点,对sql数据库的连接操作有些小区别。但有一点,那就是,对sql数据库的操作语句sql语句大体是一样的。
这段时间正进行VB.NET的学习,说实话,在最开始的时候,确实会感觉到比较难下手。在此之前,学习的是C#,从C#到VB.NET,弯不大,但有点急。现将一些VB.NET中sql数据库的增、删、改、查总结一下,方便自己以后查询,也为刚接触VB.NET的朋友提供小小参考。对于这些语句,就是一个熟练过程,更多的是运用而不是记忆。下面看看VB.NET实现增删改查的整个过程及对应实例。
查寻数据:SELECT返回的记录1、记录2、记录3……[或*]FROM表名WHERE字段名=’”变量名”’
插入数据:INSERTINTO表名(字段名1,字段名2,字段名3……)Values(@字段名1,@字段名2,@字段名3……)
更新数据:UPDATA表名SET<字段名1=参数名1><,字段名2=参数名2><,……>WHERE<条件1><,条件2><,条件2>
删除数据:DELETEFROM<表名>WHERE<条件>
对应一些实例
(1)查找数据
(采用:拼接字符串法)
Function selectlogon(ByVal User As Entity.logonEntity) As Entity.logonEntity Dim cmd_logon As New sqlCommand '实例化一个命令对象 Dim sqlconnection_logon = New sqlConnection(DbUtil.sqlConcectStr) '实例化带条件的一个数据库连接对象 cmd_logon.Connection = sqlconnection_logon '创建连接命令 sqlconnection_logon.Open() '执行连接,将数据库打开 '建立查询语句 cmd_logon.CommandText = "SELECT * FROM T_logon WHERE CardID = '" & User.CardID & "' " Dim reader As sqlClient.sqlDataReader '建立读数据对象 reader = cmd_logon.ExecuteReader '对象逐条读 Dim users As New Entity.logonEntity While (reader.Read()) '循环读数据 If users Is Nothing Then users = New Entity.logonEntity End If users.ID = reader.GetValue(reader.GetOrdinal("UserID")) '读取数据,赋给新实体 users.CardID = reader.GetValue(reader.GetOrdinal("CardID")) users.UserName = reader.GetValue(reader.GetOrdinal("UserName")) users.Sex = reader.GetValue(reader.GetOrdinal("Sex")) users.Department = reader.GetValue(reader.GetOrdinal("Department")) users.Grade = reader.GetValue(reader.GetOrdinal("Grade")) users.InCash = reader.GetValue(reader.GetOrdinal("InCash")) End While sqlconnection_logon.Close() Return users End Function
(2)插入数据
(采用:参数法)
Function InsertUp_Doing(ByVal User As Entity.MainEntity) As Boolean Dim cmd_insert_updoing As New sqlCommand Dim sqlconnection_updoing As New sqlConnection(DbUtil.sqlConcectStr) cmd_insert_updoing.Connection = sqlconnection_updoing '插入语句 cmd_insert_updoing.CommandText = "INSERT INTO T_Updoing (CardID) Values(@CardID)" '参数法 cmd_insert_updoing.Parameters.Add(New sqlParameter("@CardID",User.CardID)) sqlconnection_updoing.Open() cmd_insert_updoing.ExecuteNonQuery() sqlconnection_updoing.Close() Return True End Function
(3)更新数据
(采用:拼接字符串 法)
Function update_updown(ByVal User As Entity.MainEntity) As Boolean Dim OK_NO_updown As Boolean Dim cmd_update_updown As New sqlCommand Dim sqlconnection_updown As New sqlConnection(DbUtil.sqlConcectStr) '根据“学号”、“状态”条件更新 cmd_update_updown.Connection = sqlconnection_updown cmd_update_updown.CommandText = "UPDATE T_UpDown " & _ "SET Downdatetime = @Downdatetime,Downtime = @Downtime,Consumetime = @Consumetime" & _ ",ConsumeCash = @ConsumeCash,Remaincash = @Remaincash " & _ "WHERE CardID = '" & User.CardID & "'" cmd_update_updown.CommandType = CommandType.Text sqlconnection_updown.Open() '打开连接 '添加参数 cmd_update_updown.Parameters.Add(New sqlParameter("@Downdatetime",User.Downdatetime)) cmd_update_updown.Parameters.Add(New sqlParameter("@Downtime",User.Downtime)) cmd_update_updown.Parameters.Add(New sqlParameter("@Consumetime",User.Timeconsume)) cmd_update_updown.Parameters.Add(New sqlParameter("@ConsumeCash",User.Cashconsume)) cmd_update_updown.Parameters.Add(New sqlParameter("@Remaincash",User.Cashremain)) cmd_update_updown.ExecuteNonQuery() sqlconnection_updown.Close() Return OK_NO_updown = True End Function
(4)删除数据
(采用:拼接字符串法[CardID =' " & User.CardID & " ' ])
Function deleteUp_doing(ByVal User As Entity.MainEntity) As Boolean Dim OK_NO_down As Boolean Dim cmd_delete_updoing As New sqlCommand Dim sqlconnection_delectupdoing As New sqlConnection(DbUtil.sqlConcectStr) sqlconnection_delectupdoing.Open() cmd_delete_updoing.Connection = sqlconnection_delectupdoing cmd_delete_updoing.CommandText = "DELETE FROM T_Updoing WHERE CardID ='" & User.CardID & "'" cmd_delete_updoing.ExecuteNonQuery() sqlconnection_delectupdoing.Close() Return OK_NO_down = True End Function
在VB.NET中,与我之前使用一年前使用VB相比,多了用参数法。查了一些资料,告知,用参数法能减少错误的产生,增强安全性。
上面的代码中,传参采用的是传实体的方式。在VB.NET中,与实体代码更具自动性,与C#中实体相比,这也体现了一定的优越性。