vb.net的数据库连接
1·绑定数据源来进行连接
华州网站建设公司创新互联公司,华州网站设计制作,有大型网站制作公司丰富经验。已为华州成百上千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的华州做网站的公司定做!
2.用代码连接
先到数据库建立一个数据库和相应的表
连接数据库的代码:
Dim str As String = "Data Source=服务器名;Initial Catalog=数据库名;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=服务器名;Initial Catalog=数据库名;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() '显示下个窗体
Me.Hide() ’隐藏当前窗体
Else
MessageBox.Show("输入信息有误!", "提示")
TextBox1.Text = ""
TextBox2.Text = ""
End If
VB.Net数据库编程的问题
你的这个只写删除datagrid表格里的数据啦...跟本就没有写删除数据库的.也没有更新数据库的.
以下是删除按钮的代码..
If MsgBox("数据删除不可恢复,确认删除数据么?", vbYesNo) = vbYes Then
rec.Delete
rec.MoveNext
rec.Requery
End If
以下是form窗体的datagrid显示数据表的代码:
If rec.State = adStateOpen Then rec.Close
SQL = "select * from jbxx" '定义sql查询语句
rec.Open SQL, con, adOpenStatic, adLockOptimistic '打开记录集
Set dg.DataSource = rec '用datagrid控件显示记录集
以下是模块..
Public con As New Connection '定义数据连接,公共变量
Public coon, coom As String
Sub Main()
ChDir App.Path '转换相对路径
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=cq.mdb;Persist Security Info=False"
'定义数据库连接
con.CursorLocation = adUseClient '以客户端的方式打开
con.Open ' 打开连接
登陆.Show
End Sub
希望能帮到你...
vb.net+数据库编程问题
使用OleDb将数据库绑定datagridview,然后添加RowHeaderMouseClick事件,在此事件中定义选中某一行后在textbox中显示相应的数据,在添加的按钮中定义打开form2,用sql语句查询数据库得到想要的关键字所关联的数据,显示出来就可以了。这里我给你一部分我写过的代码,基本上和你的要求很像,但是不完全一样,你自己研究研究,改改应该就可以了。
'datagridview绑定数据库
Public Class form1
Private ObjetConnection As OleDbConnection
Private ObjetCommand As OleDbCommand
Private ObjetDataAdapter As OleDbDataAdapter
Private ObjetSet As New DataSet()
' SQL语句
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
此处添加显示表中相应信息
Me.TextBox1.text = DataGridView1.SelectedRows(0).Cells(0).Value
Me.TextBox2.text = DataGridView1.SelectedRows(0).Cells(1).Value
.........
End Sub
分享名称:vbnet数据库编程 vb数据库编程教程
网页URL:http://lswzjz.com/article/ddogddg.html