vb.net与sql sever数据库如何绑定(新手求助)

自己写了个登录窗口,现在想与sql数据库绑定,运行vb.net程序时,希望通过读取数据库里的资源实现登录,但是不知道怎么将程序和sql数据库建立联系,希望大神说的清楚点,比如sql需要放在那里,vb.net代码中添加声明函数,求帮助

1个回答

'Public publicstr As String
Public conn As ADODB.Connection
Public rs As ADODB.Recordset

Public Sub main() '数据库连接共享函数
Set conn = New ADODB.Connection
conn.Open "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=plgl;Initial Catalog=plgl"
'publicstr = "Provider=MSDASQL.1;Persist Security Info=False;User ID=sa;Data Source=plgl;Initial Catalog=plgl" '共享连接字符串
main_pfm.Show
End Sub

添加程序:

Set rs = New ADODB.Recordset
If Trim(Text1(0).Text) = "" Then
smeg = "配方号不能为空!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(0).SetFocus
Else
If Not IsNumeric(Text1(0).Text) Then
smeg = "A添加剂非数字!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(0).Text = ""
Text1(0).SetFocus
Else
If Trim(Text1(1).Text) = "" Then
smeg = "配方名称不能为空!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(1).SetFocus
Else
If Trim(Text1(2).Text) = "" Then
smeg = "A添加剂不能为空!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(2).SetFocus
Else
If Not IsNumeric(Text1(2).Text) Then
smeg = "A添加剂非数字!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(2).Text = ""
Text1(2).SetFocus
Else
If Trim(Text1(3).Text) = "" Then
smeg = "B添加剂不能为空!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(3).SetFocus
Else
If Not IsNumeric(Text1(3).Text) Then
smeg = "B添加剂非数字!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(3).Text = ""
Text1(3).SetFocus
Else
If Trim(Text1(4).Text) = "" Then
smeg = "C添加剂不能为空!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(4).SetFocus
Else
If Not IsNumeric(Text1(4).Text) Then
smeg = "C添加剂非数字!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(4).Text = ""
Text1(4).SetFocus
Else
If Trim(Text1(5).Text) = "" Then
smeg = "K系数不能为空!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(5).SetFocus
Else
If Not IsNumeric(Text1(5).Text) Then
smeg = "K系数非数字!!!"
MsgBox smeg, vbOKCancel + vbExclamation, "警告"
Text1(5).Text = ""
Text1(5).SetFocus
Else
sql = "select * from pfb where 配方号='" & Text1(0).Text & "'"
rs.Open sql, conn, 3, 3
If rs.EOF Then
rs.AddNew
rs("配方号") = Text1(0).Text
rs("配方名称") = Text1(1).Text
rs("A添加剂") = Text1(2).Text
rs("B添加剂") = Text1(3).Text
rs("C添加剂") = Text1(4).Text
rs("K系数") = Text1(5).Text
MsgBox "添加成功!!!", vbOKOnly
rs.Update
rs.Close
Set rs = Nothing
'**********************************************调整修改后再显示时能实时更新
Set rs = New ADODB.Recordset
sql = "select * from pfb"
rs.Open sql, conn, 3, 3
rs.Update
Set show_allpf.MSHFlexGrid1.DataSource = rs
'show_allpf.Visible = True
rs.Close
Else
MsgBox "此序号已经存在!", vbOKOnly + vbExclamation, "警告"
Text1(0).SetFocus
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
'Command2.Enabled = False '不能重复保存

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问