大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
下面这段代码中,myReader,myCon的关闭应在WHILE循环外。这还不是主要问题,如果你只读取首行首列不要用OleDbDataReader,直接用myCommand.ExecuteScalar就可以了,只要判断一下myCommand.ExecuteScalar返回是否为nothing就行。效率会高很多。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、虚拟主机、营销软件、网站建设、拉萨网站维护、网站推广。
While myReader.Read
If myReader.Item(0) Is System.DBNull.Value Then
Return ""
Else
Return myReader.Item(0)
End If
myReader.Close()
myCon.Close()
End While
使用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
你的这个只写删除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
希望能帮到你...
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