VB.net与SQL数据库连接
那位大侠帮个忙!代码绑定窗体(form)的datagridview1控件上!急用 --------------------编程问答-------------------- LZ是什么意思?要用语句来实现绑定到datagridview控件么?
--------------------编程问答--------------------
Private sub get_data
Dim ds As New DataSet
Dim connstr As string ="" '数据库连接字段
Using conn As New SqlConnection(connstr)
Dim mycomm As SqlCommand = conn.CreateCommand
Dim str As String = "select * from XXXX" 'sql语句
mycomm.CommandText = str
Dim sda As New SqlDataAdapter(mycomm)
sda.Fill(ds, "datatable1")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "datatable1"
end sub
--------------------编程问答--------------------
Dim sql As String
sql = " select a.porder+'-'+cast(a.eda as varchar(4)) as 订号," & _
" a.code as 项目编码,c.name as 名称,c.tani1 as 单位,a.vendor as 制造担当," & _
" a.jitu0 as 计划数量,a.lotname as 批号,substring(a.hopedate,1,8) as 希望纳期" & _
" from XKACT a " & _
" left join (select * from xkout where wcode in ('8014','8114')) b on a.code =b.code and a.bumo=b.vendor" & _
" left join xhead c on a.code=c.code" & _
" left join xsect d on a.vendor=d.bumo" & _
" where(Left(a.fdate, 8) = '" & Sdate & "' And a.kban = b.kban)" & _
" order by porder"
Dim Connection As New System.Data.SqlClient.SqlConnection(" Data Source=192.168.0.5;Initial Catalog=tpicsthtest;User ID=sa;password=sa")
Dim DataAdapter As System.Data.SqlClient.SqlDataAdapter
Connection.Open()
DataAdapter = New SqlClient.SqlDataAdapter(sql, Connection)
DataSet = New DataSet
Application.DoEvents()
DataAdapter.Fill(DataSet, "DATA")
Connection.Close()
datagridview1.datasourse=DataSet.tables(0)
ps:最后漏了个end using 楼主记得补上
比较懒,都写着一个方法里了,也没什么注释,LZ具体实施的时候可以把数据库操作单独放个方法里,或者放在function里,然后返回一个dataset也可以 --------------------编程问答--------------------
--------------------编程问答-------------------- 不凑热闹了,都很详细,不抢分了,哈哈 --------------------编程问答-------------------- http://blog.csdn.net/JiaoPengJay/archive/2010/02/03/5285252.aspx
Dim sqlConnection1 As SqlClient.SqlConnection
Dim strCommand As SqlClient.SqlCommand
Dim DataSet1 As New DataSet
Dim strConnect As String = "Data Source=LP;Initial Catalog=WJH;Integrated Security=True;"
sqlConnection1 = New System.Data.SqlClient.SqlConnection(strConnect)
sqlConnection1.Open() '打开数据库
strCommand = sqlConnection1.CreateCommand
Dim strsql As String
strCommand.CommandText = "select * from dbo.Text "
Dim sda As New SqlDataAdapter(strCommand)
sda.Fill(DataSet1, "tb1")
DataGridView1.DataSource = DataSet1
DataGridView1.DataMember = "tb1"
sqlConnection1.Close()
这里有DataGridView连接数据库的还有一些在DataGridView中直接进行操作(增删改查,不过有点缺陷,嘿嘿)
可能对你有所帮助 --------------------编程问答-------------------- Dim sqlConnection1 As SqlClient.SqlConnection
Dim strCommand As SqlClient.SqlCommand
Dim DataSet1 As New DataSet
Dim strConnect As String = "Data Source=LP;Initial Catalog=WJH;Integrated Security=True;"
sqlConnection1 = New System.Data.SqlClient.SqlConnection(strConnect)
sqlConnection1.Open() '打开数据库
strCommand = sqlConnection1.CreateCommand
Dim strsql As String
strCommand.CommandText = "select * from dbo.Text "
Dim sda As New SqlDataAdapter(strCommand)
sda.Fill(DataSet1, "tb1")
DataGridView1.DataSource = DataSet1
DataGridView1.DataMember = "tb1"
sqlConnection1.Close()
msdn上面有的 --------------------编程问答-------------------- Dim connstr As String = "server=.;uid=sa;pwd=sa;database=Test"
Using conn As New SqlConnection(connstr)
conn.Open()
Dim ds As New DataSet()
Dim da As New SqlDataAdapter(sqlsel, conn)
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
conn.Close()
End Using --------------------编程问答-------------------- 不错,路过学习 --------------------编程问答-------------------- 恩,不错 --------------------编程问答-------------------- 这个就可以 --------------------编程问答-------------------- 都回答了,up --------------------编程问答-------------------- 嗯 不错不错 有用 偶要仔细学学 --------------------编程问答-------------------- 菜单栏-添加新数据源-下一步-新建连接。。。 你试下 --------------------编程问答-------------------- dataset ds=new dataset();
using(sqldataadapter adp=new ("select * from xxx","connectionStr"))
{
adp.fill(ds);
}
if(ds==null||ds.table[0].rows.cout<=0)
return;
datagrid1.datasource=ds.table[0]; --------------------编程问答--------------------
bucuo .bucuo --------------------编程问答-------------------- 新手学习了!!!新手学习了!!!
补充:.NET技术 , VB.NET