VB.NET 2008中 如何在DataGridView控件中显示数据查询的结果?数据库是SQL Server2008 20
在Form1中添加的控件为:Button1,TextBox1,DataGridView1.查询按钮(Button1)代码如下:DimLeafSqlConnectionAs...
在Form1中添加的控件为:Button1,TextBox1,DataGridView1.
查询按钮(Button1)代码如下:
Dim LeafSqlConnection As SqlConnection '声明这些SQL的类
Dim LeafSqlCommand As SqlCommand
Dim LeafSqlDataAdapter As SqlDataAdapter
Dim LeafData As DataTable
LeafSqlConnection = New SqlConnection("Data Source=BORISWANG-PC;AttachDbFilename=E:\WANGJ\Project\originalfiles\VIPcardDatabase 3.mdf;Persist Security Info=True;User ID=sa;Password=asd")
LeafSqlConnection.Open()
LeafSqlCommand = New SqlCommand("Select * from TableMember where 会员姓名= '" & TextBox1.Text & "'", LeafSqlConnection)
LeafSqlDataAdapter = New SqlDataAdapter
LeafSqlCommand.CommandType = CommandType.Text
LeafSqlDataAdapter.SelectCommand = LeafSqlCommand
LeafData = New DataTable
LeafSqlDataAdapter.Fill(LeafData)
TableMemberDataGridView.DataSource = LeafData
可是,运行时DataGridView中是没有东西的,其中TableMember表中有会员姓名为“wj”的记录。
拜托大家帮帮忙吧,我实在弄不出了~ 展开
查询按钮(Button1)代码如下:
Dim LeafSqlConnection As SqlConnection '声明这些SQL的类
Dim LeafSqlCommand As SqlCommand
Dim LeafSqlDataAdapter As SqlDataAdapter
Dim LeafData As DataTable
LeafSqlConnection = New SqlConnection("Data Source=BORISWANG-PC;AttachDbFilename=E:\WANGJ\Project\originalfiles\VIPcardDatabase 3.mdf;Persist Security Info=True;User ID=sa;Password=asd")
LeafSqlConnection.Open()
LeafSqlCommand = New SqlCommand("Select * from TableMember where 会员姓名= '" & TextBox1.Text & "'", LeafSqlConnection)
LeafSqlDataAdapter = New SqlDataAdapter
LeafSqlCommand.CommandType = CommandType.Text
LeafSqlDataAdapter.SelectCommand = LeafSqlCommand
LeafData = New DataTable
LeafSqlDataAdapter.Fill(LeafData)
TableMemberDataGridView.DataSource = LeafData
可是,运行时DataGridView中是没有东西的,其中TableMember表中有会员姓名为“wj”的记录。
拜托大家帮帮忙吧,我实在弄不出了~ 展开
2个回答
展开全部
LeafSqlCommand = New SqlCommand("Select * from TableMember where 会员姓名= '" & TextBox1.Text & "'", LeafSqlConnection) 你这句是不是写错了啊
LeafSqlCommand = New SqlCommand("Select * from TableMember where 会员姓名= “ & ”‘“ & tBox1.Text & "'", LeafSqlConnection)
LeafSqlCommand = New SqlCommand("Select * from TableMember where 会员姓名= “ & ”‘“ & tBox1.Text & "'", LeafSqlConnection)
追问
不是哦,你这样的话语法都不对~
主要是 我试过直接写成Select * from TableMember where 会员姓名= 'wj',也是一样的运行结果,不出东西,我想应该不是连接字符串的错误吧
追答
不是吧,你尝试我的了吗,我怎么一直都是这样写的,给你看看我的代码,运行很正常啊。
Dim Conn As New SqlConnection("data source=(local)\sqlexpress;initial catalog=database;integrated security=true")
Conn.Open()
Dim comm As New SqlCommand("select * from 信息数据库 where 名字=" & "'" & ComboBox1.Text & "'", Conn)
Dim adpt As SqlDataAdapter
Dim ds As New DataSet
Dim BS As New BindingSource
adpt = New SqlDataAdapter(comm)
adpt.Fill(ds, "信息数据库")
BS.DataSource = ds.Tables("信息数据库")
DataGridView1.DataSource = BS
展开全部
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oledb1 As New OleDb.OleDbConnection
Dim ServerIP as string="127.0.0.1"
oledb1.ConnectionString = "Provider=SQLOLEDB;Data Source=" + ServerIP + ";Initial Catalog=VIPcardDatabase 3;User ID=sa;Password=asd"
oledb1.Open()
If ConnectionState.Open Then
Dim ssql As String
ssql = "Select * from TableMember"
Dim MyAdapter As New OleDb.OleDbDataAdapter(ssql, oledb1)
Dim MyTable As New DataTable()
MyAdapter.Fill(MyTable)
DataGridView1.DataSource = MyTable
oledb1.Close()
'MsgBox("查询成功")
End If
End Sub
Dim oledb1 As New OleDb.OleDbConnection
Dim ServerIP as string="127.0.0.1"
oledb1.ConnectionString = "Provider=SQLOLEDB;Data Source=" + ServerIP + ";Initial Catalog=VIPcardDatabase 3;User ID=sa;Password=asd"
oledb1.Open()
If ConnectionState.Open Then
Dim ssql As String
ssql = "Select * from TableMember"
Dim MyAdapter As New OleDb.OleDbDataAdapter(ssql, oledb1)
Dim MyTable As New DataTable()
MyAdapter.Fill(MyTable)
DataGridView1.DataSource = MyTable
oledb1.Close()
'MsgBox("查询成功")
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询