vb.net 连接sql数据库
Form1窗口窗口里面有控件ComboBox1和DataGridView1Module1模块连接数据库的代码全部在Module1模块里,在form1只需要调用模块里就可以...
Form1窗口
窗口里面有控件ComboBox1和DataGridView1
Module1模块
连接数据库的代码全部在Module1模块里,在form1只需要调用模块里就可以了
然后把数据库表”123“全部在DataGridView1里显示出来
并且把表”123“里面打第一列”456“全部添加在”ComboBox1“控件里
我要详细的代码
满意后有高分奖励
要详细的源码 展开
窗口里面有控件ComboBox1和DataGridView1
Module1模块
连接数据库的代码全部在Module1模块里,在form1只需要调用模块里就可以了
然后把数据库表”123“全部在DataGridView1里显示出来
并且把表”123“里面打第一列”456“全部添加在”ComboBox1“控件里
我要详细的代码
满意后有高分奖励
要详细的源码 展开
1个回答
展开全部
Imports System.Data
Imports System.Data.OleDb
Module Module1
Public Function MdbMytab(ByVal str_select As String) As DataTable
Dim conn_mdb As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=mdb.mdb")
Dim cmd As New OleDbCommand(str_select, conn_mdb)
Dim da As New OleDbDataAdapter(cmd)
Dim mytb As DataSet = New DataSet
da.Fill(mytb, 0)
MdbMytab = mytb.Tables(0)
conn_mdb.Close()
End Function
End Module
以上为模块代码
以下为窗体代码:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mytb As DataTable = MdbMytab("select * from 表1")
DataGridView1.DataSource = mytb
ComboBox1.DisplayMember = "姓名"
ComboBox1.DataSource = mytb
End Sub
End Class
Imports System.Data.OleDb
Module Module1
Public Function MdbMytab(ByVal str_select As String) As DataTable
Dim conn_mdb As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=mdb.mdb")
Dim cmd As New OleDbCommand(str_select, conn_mdb)
Dim da As New OleDbDataAdapter(cmd)
Dim mytb As DataSet = New DataSet
da.Fill(mytb, 0)
MdbMytab = mytb.Tables(0)
conn_mdb.Close()
End Function
End Module
以上为模块代码
以下为窗体代码:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mytb As DataTable = MdbMytab("select * from 表1")
DataGridView1.DataSource = mytb
ComboBox1.DisplayMember = "姓名"
ComboBox1.DataSource = mytb
End Sub
End Class
更多追问追答
追问
我用的是sql数据库
我已经改过来了
测试已经成功
顺便在问一下
我做了一个登陆界面
怎么比较一条数据库的三个条件(部门、用户名、密码)都满足打情况下进入系统,否则提示”登陆失败,请检查用户信息!“
追答
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Module Module1
Public Function sqlmytb(ByVal str_select As String) As DataTable
Dim conn As New SqlConnection("server=192.168.0.1;uid=sa;pwd=123334;database=????")
Dim cmd As New SqlCommand(str_select, conn)
Dim da As New SqlDataAdapter(cmd)
Dim ds = New DataSet
da.Fill(ds, 0)
sqlmytb = ds.Tables(0)
conn.Close()
End Function
End Module
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tb_xx As DataTable
tb_xx = sqlmytb("select from 表1 where 部门='" & ComboBox1.Text & "' and 用户名='" & textbox1.text & "' and 密码='" & textbox2.text & "'")
If tb_xx.Rows.Count < 1 Then
MsgBox("用户信息不存在")
Else
窗体.show()
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询