1·綁定數(shù)據(jù)源來(lái)進(jìn)行連接
華州網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,華州網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為華州成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的華州做網(wǎng)站的公司定做!
2.用代碼連接
先到數(shù)據(jù)庫(kù)建立一個(gè)數(shù)據(jù)庫(kù)和相應(yīng)的表
連接數(shù)據(jù)庫(kù)的代碼:
Dim str As String = "Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫(kù)名;Persist Security Info=True;User ID=;Password="
dim conn As SqlClient.SqlConnection
try
conn = New SqlClient.SqlConnection
conn.ConnectionString = str
conn.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
登錄代碼:Dim str As String = "Data Source=服務(wù)器名;Initial Catalog=數(shù)據(jù)庫(kù)名;Persist Security Info=True;User ID=;Password="
dim conn As SqlClient.SqlConnection
conn = New SqlClient.SqlConnection
conn.ConnectionString = str
conn.Open()
sqlstr = "Select * From Amd Where AmdName='" TextBox1.Text "' And AmdPwd = '" TextBox2.Text "'"
Dim sqlcmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(sqlstr, conn)
Dim dr As SqlClient.SqlDataReader
dr = sqlcmd.ExecuteReader
If dr.Read = True Then '判斷一條記錄為真
kf.Show() '顯示下個(gè)窗體
Me.Hide() ’隱藏當(dāng)前窗體
Else
MessageBox.Show("輸入信息有誤!", "提示")
TextBox1.Text = ""
TextBox2.Text = ""
End If
你的這個(gè)只寫刪除datagrid表格里的數(shù)據(jù)啦...跟本就沒(méi)有寫刪除數(shù)據(jù)庫(kù)的.也沒(méi)有更新數(shù)據(jù)庫(kù)的.
以下是刪除按鈕的代碼..
If MsgBox("數(shù)據(jù)刪除不可恢復(fù),確認(rèn)刪除數(shù)據(jù)么?", vbYesNo) = vbYes Then
rec.Delete
rec.MoveNext
rec.Requery
End If
以下是form窗體的datagrid顯示數(shù)據(jù)表的代碼:
If rec.State = adStateOpen Then rec.Close
SQL = "select * from jbxx" '定義sql查詢語(yǔ)句
rec.Open SQL, con, adOpenStatic, adLockOptimistic '打開記錄集
Set dg.DataSource = rec '用datagrid控件顯示記錄集
以下是模塊..
Public con As New Connection '定義數(shù)據(jù)連接,公共變量
Public coon, coom As String
Sub Main()
ChDir App.Path '轉(zhuǎn)換相對(duì)路徑
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=cq.mdb;Persist Security Info=False"
'定義數(shù)據(jù)庫(kù)連接
con.CursorLocation = adUseClient '以客戶端的方式打開
con.Open ' 打開連接
登陸.Show
End Sub
希望能幫到你...
使用OleDb將數(shù)據(jù)庫(kù)綁定datagridview,然后添加RowHeaderMouseClick事件,在此事件中定義選中某一行后在textbox中顯示相應(yīng)的數(shù)據(jù),在添加的按鈕中定義打開form2,用sql語(yǔ)句查詢數(shù)據(jù)庫(kù)得到想要的關(guān)鍵字所關(guān)聯(lián)的數(shù)據(jù),顯示出來(lái)就可以了。這里我給你一部分我寫過(guò)的代碼,基本上和你的要求很像,但是不完全一樣,你自己研究研究,改改應(yīng)該就可以了。
'datagridview綁定數(shù)據(jù)庫(kù)
Public Class form1
Private ObjetConnection As OleDbConnection
Private ObjetCommand As OleDbCommand
Private ObjetDataAdapter As OleDbDataAdapter
Private ObjetSet As New DataSet()
' SQL語(yǔ)句
Private strSql As String
Private ObjetDataTable As DataTable
Private ObjetDataRow As DataRow
Private Numeroligne As Integer
'定義路徑
Private strConn As String
Private ObjetCommandBuilder As OleDbCommandBuilder
Dim dv As New DataView
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
strConn =
"Provider=Microsoft.ACE.OLEDB.12.0; Data source=" Application.StartupPath "\文件名.accdb" /此處文件路徑
strSql =
"Select * 表名 "
ObjetConnection =
New OleDbConnection()
ObjetConnection.ConnectionString = strConn
ObjetConnection.Open()
ObjetCommand =
New OleDbCommand(strSql)
ObjetDataAdapter =
New OleDbDataAdapter(ObjetCommand)
ObjetCommand.Connection() = ObjetConnection
ObjetDataAdapter.Fill(ObjetSet,
"表明")
dv.Table = ObjetSet.Tables(
"表名")
DataGridView1.DataSource = dv
ObjetConnection.Close()
End Sub
/此處是RowHeaderMouseclick事件
Private Sub DataGridView1_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
此處添加顯示表中相應(yīng)信息
Me.TextBox1.text = DataGridView1.SelectedRows(0).Cells(0).Value
Me.TextBox2.text = DataGridView1.SelectedRows(0).Cells(1).Value
.........
End Sub