VB.NET 连接数据库后的操作

连接到数据库后用
Command.CommandText ="Inssert Into (Tbale1) value ('Value1')"
等语句后数据库里没有新增内容,删除也一样,不知道是不是还需要什么操作或者哪里错了.
源代码:
Private Sub tst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tst.Click
Dim AConnect As OleDb.OleDbConnection
Dim ACmd As OleDb.OleDbCommand
Dim ConnectStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Enroll.mdb"
Dim AFind As String = "insert into rj (编号) value ('1000')"
AConnect = New OleDb.OleDbConnection(ConnectStr)
AConnect.Open()
ACmd = New OleDb.OleDbCommand(AFind, AConnect)
ACmd.CommandText = AFind
AConnect.Close()
End Sub

对数据库的查询返回结果怎么写,如果需要把相关值返回到DataGridView里或者其它的控件中。

ACmd.CommandText = AFind 下面要加上一句执行
ACmd.ExecuteNonQuery()
-------------------------------------
要写查询到控件DataGridView的话,可以这样做:.net2.0或以上的环境中:
Dim AConnect As OleDb.OleDbConnection
Dim ACmd As OleDb.OleDbCommand
Dim dtResult As DataTable=new DataTable()
Dim ConnectStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\Enroll.mdb"
Dim AFind As String = "SELECT * FROM rj"
AConnect = New OleDb.OleDbConnection(ConnectStr)
AConnect.Open()
ACmd = New OleDb.OleDbCommand(AFind, AConnect)
ACmd.CommandText = AFind
Dim ADr as OleDb.OleDbDataReader=ACmd.ExcuteReader()
dtResult.Load(ADr)
AConnect.Close()
DataGridView.DataSource=dtResult
---------
希望能符合你的需求
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-07-18
下面是我的两个函数,希望对你又帮助:
Function Getdata(ByVal Sqlstr As String) As DataTable '取得数据
Dim Connstr As String = "provider=SQLOLEDB;data source=" & ServerName & ";initial catalog=" & DBName & ";user id =" & DBUserName & ";password=" & DBUserPwd
Dim SqlConn As New OleDb.OleDbConnection(Connstr)
Dim SqlCmd As New OleDb.OleDbCommand(Sqlstr, SqlConn)
Dim TmpDst As New DataSet
Dim TmpAda As New OleDb.OleDbDataAdapter
TmpAda.TableMappings.Add("table", "temp")
TmpAda.SelectCommand = SqlCmd
SqlConn.Open()
SqlCmd.ExecuteNonQuery()
SqlConn.Close()
TmpAda.Fill(TmpDst, "temp")
Return TmpDst.Tables("temp")
End Function

Function Gengxindata(ByVal Sqlstr As String) As Boolean '更新数据
Try
Dim Connstr As String = "Provider=SQLOLEDB;data source=" & ServerName & ";initial catalog=" & DBName & ";user id =" & DBUserName & ";password=" & DBUserPwd
Dim SqlConn As New OleDb.OleDbConnection(Connstr)
Dim SqlCmd As New OleDb.OleDbCommand(Sqlstr, SqlConn)
SqlConn.Open()
SqlCmd.ExecuteNonQuery()
SqlConn.Close()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
End Try
End Function

看你连接的是access数据库,只要把函数里面的链接字符串改一下就可以了!
对数据库的查询返回结果:
dim TSqlStr1 as string="你的sql语句"
datagridview.datasource=getdata(TSqlStr1)'getdata()是上面第一个函数
第2个回答  2009-07-17
好复杂的问题了。。。
百度上是不太可能问得到,建议楼主到专业点的论坛上发帖问比较好~~
相似回答