数据蕴含丰富的信息,数据就是资源。
不同的语言,由于各自的语法特点,对sql数据库的连接操作有些小区别。但有一点,那就是,对sql数据库的操作语句sql语句大体是一样的。
这段时间正进行VB.NET的学习,说实话,在最开始的时候,确实会感觉到比较难下手。在此之前,学习的是C#,从C#到VB.NET,弯不大,但有点急。现将一些VB.NET中sql数据库的增、删、改、查总结一下,方便自己以后查询,也为刚接触VB.NET的朋友提供小小参考。对于这些语句,就是一个熟练过程,更多的是运用而不是记忆。下面看看VB.NET实现增删改查的整个过程及对应实例。
查寻数据:SELECT 返回的记录1、记录2、记录3……[或*] FROM 表名 WHERE 字段名 =’ ”变量名” ’
插入数据:INSERT INTO 表名 (字段名1,字段名2,字段名3……) Values(@字段名1,@字段名2,@字段名3……)
更新数据:UPDATA 表名 SET <字段名1=参数名1> <, 字段名2=参数名2> <, ……> WHERE <条件1> <,条件2> <,条件2>
删除数据:DELETE FROM <表名> 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
(采用:拼接字符串 法)
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#中实体相比,这也体现了一定的优越性。