重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先在项目的VB.NET界面,使用菜单【项目】--【添加引用】--【COM】
创新互联建站主打移动网站、做网站、网站设计、网站改版、网络推广、网站维护、国际域名空间、等互联网信息服务,为各行业提供服务。在技术实力的保障下,我们为客户承诺稳定,放心的服务,根据网站的内容与功能再决定采用什么样的设计。最后,要实现符合网站需求的内容、功能与设计,我们还会规划稳定安全的技术方案做保障。
选择 Microsoft ADO Ext. 2.x for DDL and Security
然后单击【确定】,完成引用。
完整代码如下:
Imports ADOX
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建空的access数据库文件--数据库文件.mdb,密码为123
Dim Mycat As Catalog = New Catalog()
Mycat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source= 数据库文件.mdb;Jet OLEDB:Database Password=123")
'以下代码创建一个名为“实验数据表”
Dim MyTable As ADOX.Table = New ADOX.Table '定义新表
MyTable.Name = "实验数据表" '表命名
'给表“实验数据表” 创建一个字符串字段,字段名“姓名”
MyTable.Columns.Append("姓名", , ADOX.DataTypeEnum.adWChar)
'给表“实验数据表” 创建一个整数字段,字段名“学号”
MyTable.Columns.Append("学号", ADOX.DataTypeEnum.adInteger) '追加一个数字型字段
'给字段“学号”创建一个主键“PimaryKey_Field”
MyTable.Keys.Append("学号", ADOX.KeyTypeEnum.adKeyPrimary, "学号")
Mycat.Tables.Append(MyTable) '把所有的新字段追加到表
MyTable = Nothing
Mycat = Nothing
End Sub
End Class
你这是向sql后台数据库写数据吗?
sqldataadapter1.update 改须指定有效的updatecommand,我一直没有看到
给你个例子你看吧:
'首先指定有效的UpdateCommand
sqlstr = "insert into Playlist(PlayName,PlayPath)values('" TextBox4.Text.Trim "','" TextBox5.Text.Trim "')"
Module1.Mycommand.CommandText = sqlstr
'增加新一行(此行必须,要不不能与后台同步,并且后面调用update会出错)
Module1.MyDataSet.Tables("Music").Rows.Add()
Module1.MyDataAdaPter.InsertCommand = Module1.Mycommand
Module1.MyDataAdaPter.Update(Module1.MyDataSet, "Music")
MsgBox("增加数据成功!!")
使用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
Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click 备份数据库 BACKUP DATABASE Test TO DISK= C:\Backup WITH Format Dim cn As SqlConnection Dim cm As SqlCommand cn = getDB() cm = New SqlCommand( BACKUP DATABASE JXbase TO DISK= C:\Backup WITH Format cn) cn Open() cm ExecuteNonQuery() MsgBox( 备份成功 ) cn Close()
End Sub
恢复 Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click Dim cn As New SqlConnection Dim cn As New SqlConnection Dim mydr As SqlDataReader Dim str As String
Dim tmpConnectionString As String = Data Source=JJK;Initial Catalog=master;Integrated Security=True cn ConnectionString = tmpConnectionString cn ConnectionString = tmpConnectionString cn Open() cn Open() Dim cm As SqlCommand = New SqlCommand( select spid from master sysprocesses where dbid=db_id( JXbase ) cn) mydr = cm ExecuteReader()
Dim cm As SqlCommand = New SqlCommand() cm Connection = cn
While (mydr Read()) 杀死进程
str = kill mydr( spid ) ToString() cm CommandText = str cm CommandType = CommandType Text Application DoEvents() cm ExecuteNonQuery() 杀进程 End While mydr Close() cm = New SqlCommand( RESTORE DATABASE JXbase FROM DISK= C:\Backup cn) 恢复备份 cm ExecuteNonQuery() MsgBox( 恢复成功成功 ) cn Close() cn Close()
lishixinzhi/Article/program/net/201311/12901
这也是我研究很长时间的成果,差不多分得给我吧。\(^o^)/~
Imports System.Data
Public Class Form1
Public dt As New DataTable
Public dr As DataRow
'内存表格及界面表格初始化,分类列表框初始化
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetDesktopBounds(283, 84, 800, 600)
With dt
.Clear()
.Columns.Add("ID")
.Columns.Add("故障描述")
.Columns.Add("原因分析")
.Columns.Add("排除办法")
.Columns.Add("备注")
.Columns.Add("时间")
End With
With Me.DataGridView1
.DataSource = dt
.Columns(0).Width = 30
.Columns(1).Width = 120
.Columns(2).Width = 150
.Columns(3).Width = 190
.Columns(4).Width = 65
.Columns(5).Width = 150
End With
With TS_ComboBox1
.Items.Clear()
.Items.Add("电气")
.Items.Add("机械")
.Items.Add("操作")
End With
End Sub
'将表格中的数据显示到文本框中
Private Sub DataGridView1_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If Not IsDBNull(DataGridView1.CurrentRow.Cells(1).Value) Then
RichTextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(2).Value) Then
RichTextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(3).Value) Then
RichTextBox3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
If Not IsDBNull(DataGridView1.CurrentRow.Cells(4).Value) Then
RichTextBox4.Text = DataGridView1.CurrentRow.Cells(4).Value
End If
End Sub
Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click
Dim str
str = MsgBox("您确定要退出系统吗?", vbOKCancel + vbQuestion, "系统提示")
If str = vbOK Then
End
End If
End Sub
'查看所有记录
Private Sub MenuItem2_1_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2_1_1.Click
Dim strsql
Dim rs As OleDb.OleDbDataReader
strsql = "select * from Fault_Analyse order by ID"
transactsql(strsql)
rs = oledbcmd.ExecuteReader
dt.Clear()
Do Until Not rs.Read() '在这里作为条件的同时,也执行了该语句
dr = dt.NewRow
dr.Item(0) = rs.Item(0)
dr.Item(1) = rs.Item(2)
dr.Item(2) = rs.Item(3)
dr.Item(3) = rs.Item(4)
dr.Item(4) = rs.Item(5)
dr.Item(5) = rs.Item(1)
dt.Rows.Add(dr)
Loop
DataGridView1.Refresh()
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
End Sub
'工具栏隐藏与显示操作
Private Sub MenuItem3_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3_1.Click
If Me.MenuItem3_1.Checked = True Then
Me.MenuItem3_1.Checked = False
Else
Me.MenuItem3_1.Checked = True
End If
If Me.MenuItem3_1.Checked = True Then
ToolStrip1.Visible = True
Else
ToolStrip1.Visible = False
End If
End Sub
'往Access数据库中插入新行
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
Dim strsql As Object
Dim rs As OleDb.OleDbDataReader
Dim datetime As Date
Dim describe_fault, analyse_reason, solution, remarks As String
Dim class_g1, class_g2 As String
Dim i, j As Integer
i = 0 '初始化
describe_fault = RichTextBox1.Text
analyse_reason = RichTextBox2.Text
solution = RichTextBox3.Text
remarks = RichTextBox4.Text
class_g1 = TS_ComboBox1.Text
class_g2 = TS_ComboBox2.Text
datetime = Now
'注意格式
strsql = "select ID from Fault_Analyse" 'where ID =" + ID + ""
transactsql(strsql)
rs = oledbcmd.ExecuteReader
Do Until Not rs.Read() '获取记录的行数
i = i + 1
Loop
If i = 0 Then i = 1
For j = 1 To i '查询表中是否有ID为j的记录,没有记录则定义新行j
strsql = "select ID from Fault_Analyse where ID =" + CStr(j) + ""
transactsql(strsql)
rs = oledbcmd.ExecuteReader
If Not rs.Read() Then
Num1.Value = CInt(j)
Exit For
End If
Next
If TS_ComboBox1.Text "" And TS_ComboBox2.Text "" Then
strsql = "Insert into Fault_Analyse values('" + CStr(j) + "','" + datetime + " ','" _
+ describe_fault + " ','" + analyse_reason + " ','" + solution + " ','" _
+ remarks + "','" + class_g1 + "','" + class_g2 + "','0')"
transactsql(strsql)
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
Else
MsgBox("请为输入的内容分类。", vbOK + vbInformation, "系统提示")
End If
End Sub
'从数据库中删除选中行
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
Dim strsql
Dim str
str = MsgBox("确定要删除- " + CStr(RichTextBox1.Text) + " -的内容吗?", vbOKCancel + vbQuestion, "系统提示")
If str = vbOK Then
strsql = "delete from Fault_Analyse where describe_fault='" + CStr(RichTextBox1.Text) + "'"
transactsql(strsql)
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
End If
End Sub
'按照分类查询数据库中指定内容
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
Dim strsql
Dim rs As OleDb.OleDbDataReader
If TS_ComboBox1.Text = "" Or TS_ComboBox2.Text = "" Then
MsgBox("请选择您要查询的类别", vbOKCancel + vbInformation, "系统提示")
Else
strsql = "select * from Fault_Analyse where class_g1='" _
+ TS_ComboBox1.Text + "'and class_g2='" + TS_ComboBox2.Text + "' order by ID"
transactsql(strsql)
rs = oledbcmd.ExecuteReader
dt.Clear()
Do Until Not rs.Read() '在这里作为条件的同时,也执行了该语句
dr = dt.NewRow
dr.Item(0) = rs.Item(0)
dr.Item(1) = rs.Item(2)
dr.Item(2) = rs.Item(3)
dr.Item(3) = rs.Item(4)
dr.Item(4) = rs.Item(5)
dr.Item(5) = rs.Item(1)
dt.Rows.Add(dr)
Loop
DataGridView1.Refresh()
oledbcmd.Dispose() '释放后不能读值
dbconnection.Close()
dbconnection.Dispose()
End If
End Sub
'界面文本框内容清空
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str
str = MsgBox("确定要清空文本框中内容吗?", vbOKCancel + vbQuestion, "系统提示")
If str = vbOK Then
RichTextBox1.Text = " "
RichTextBox2.Text = " "
RichTextBox3.Text = " "
RichTextBox4.Text = " "
End If
End Sub
'分类选择列表框的定义
Private Sub TS_ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TS_ComboBox1.TextChanged
TS_ComboBox2.Text = ""
If TS_ComboBox1.Text = "电气" Then
With TS_ComboBox2.Items
.Clear()
.Add("程序")
.Add("设备")
.Add("方案")
.Add("工艺")
.Add("其它")
End With
ElseIf TS_ComboBox1.Text = "机械" Then
With TS_ComboBox2.Items
.Clear()
.Add("设计")
.Add("质量")
.Add("故障")
.Add("其它")
End With
ElseIf TS_ComboBox2.Text = "操作" Then
With TS_ComboBox2.Items
.Add("熟练度")
.Add("方案")
.Add("其它")
End With
End If
End Sub
'工具栏的显示与隐藏
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
MenuItem3_1.Checked = False
ToolStrip1.Visible = False
End Sub
End Class
你的这个只写删除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
希望能帮到你...