VB.NET 连接数据库后的操作
连接到数据库后用Command.CommandText="InssertInto(Tbale1)value('Value1')"等语句后数据库里没有新增内容,删除也一样,...
连接到数据库后用
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里或者其它的控件中。 展开
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里或者其它的控件中。 展开
3个回答
展开全部
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
---------
希望能符合你的罩轿需求
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
---------
希望能符合你的罩轿需求
展开全部
下面是我的两个函数,希望对你又帮助:
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()是上面第一个函数
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()是上面第一个函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
好复杂的问题了。。。
百度上是不慧型塌太可能问得到,建议楼主到专业点的论坛上发帖问比较前圆租猛好~~
百度上是不慧型塌太可能问得到,建议楼主到专业点的论坛上发帖问比较前圆租猛好~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询