VB.net:如何从datagridview更新数据库 [英] VB.net: How to update database from datagridview
本文介绍了VB.net:如何从datagridview更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试读取datagridview并将更改发布回SQL数据库。有没有人有一些直截了当的代码可以工作。我是VB.net的新手。
[confused]
I'm trying to read a datagridview and post the changes back to the SQL database. Does anyone have some straightforward code that will work. I'm a newbie in VB.net.
[confused]
推荐答案
嗨Roger,
当人们要求直截了当的代码时这是不可能回应的。所有代码都是根据个人需求量身定制的,没有人能够完全拥有您想要的代码。 (如果你不愿意为自己付出一些努力,他们也不会为你创造它。)开始这样一个项目的最好方法是阅读一些关于你在做什么的文章。您可以在codeproject中搜索文章,例如此。 [< a href =http://www.codeproject.com/info/search.aspx?artkw=datagridview+update+sql&sbo=kwtarget =_ blanktitle =New Window> ^ ] 。然后,当您实现文章中找到的代码时,如果遇到问题,请发布代码和错误消息,以及您的程序正在做什么以及您希望它做什么的描述。然后我们可以帮到你。希望这能为你提供一个好的起点。
Hi Roger,
When people ask for "Straightforward code" it's kind of impossible to respond. All code is tailored to an individual need and no one is going to have EXACTLY the code you want. (Nor will they create it for you if you are not willing to put some effort into it yourself.) The best way to start a project like this is to read some articles about what you are doing. You can search codeproject for articles, like this.[^]. Then as you implement code you've found in the article, if you run into problems post your code and error messages and a description of what your program is doing and what you want it to do. Then we can help you. Hope this gives you a good place to start.
我有一个文本框的访问连接作为数据库的数据馈送器,如果你愿意,可以将它改为SQL。
代码为:
I have a access connection with textbox as data feeder to database change it to SQL if u want.
The code is:
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form2
Dim conaccess As New OleDbConnection
Dim conreader As OleDbDataReader
Dim concmd As New OleDbCommand
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.EditMode = False
conaccess.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=d:\vijay.mdb"
conaccess.Open()
loadGrid()
End Sub
Private Sub loadGrid()
Dim access As String
access = "select * from vijay"
Dim DataTab As New DataTable
Dim DataAdap As New OleDbDataAdapter(access, conaccess)
DataAdap.Fill(DataTab)
DataGridView1.DataSource = DataTab
End Sub
Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click
Dim no As String
no = "select Max(ID) from vijay"
Dim concmd As New OleDbCommand(no, conaccess)
conreader = concmd.ExecuteReader
If (conreader.Read) Then
If (IsDBNull(conreader(0))) Then
id_txt.Text = "1"
Else
id_txt.Text = conreader(0) + 1
End If
name_txt.Clear()
branch_txt.Clear()
age_txt.Clear()
class_txt.Clear()
gen_txt.Clear()
End If
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
Try
id_txt.Text = DataGridView1.Item(0, i).Value
name_txt.Text = DataGridView1.Item(1, i).Value
class_txt.Text = DataGridView1.Item(2, i).Value
gen_txt.Text = DataGridView1.Item(3, i).Value
branch_txt.Text = DataGridView1.Item(4, i).Value
age_txt.Text = DataGridView1.Item(5, i).Value
Catch ex As Exception
End Try
End Sub
Private Sub del_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles del_btn.Click
Dim delcmd As New OleDbCommand("delete from vijay where id=" & id_txt.Text & " ", conaccess)
delcmd.ExecuteNonQuery()
MsgBox("Record is deleted")
loadGrid()
id_txt.Clear()
name_txt.Clear()
branch_txt.Clear()
age_txt.Clear()
class_txt.Clear()
gen_txt.Clear()
End Sub
Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
Dim access As String = String.Format("INSERT INTO vijay (Name,Class,Branch,Gender,Age) VALUES('{0}','{1}','{2}','{3}','{4}')", name_txt.Text, class_txt.Text, branch_txt.Text, gen_txt.Text, age_txt.Text)
concmd.Connection = conaccess
concmd.CommandText = access
concmd.ExecuteNonQuery()
MsgBox("Record Successfully Saved")
loadGrid()
id_txt.Clear()
name_txt.Clear()
branch_txt.Clear()
age_txt.Clear()
class_txt.Clear()
gen_txt.Clear()
End Sub
Private Sub up_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles up_btn.Click
Dim access As String
access = "UPDATE vijay SET Name = '" & name_txt.Text & "', Age = '" & age_txt.Text & "', Gender ='" & gen_txt.Text & "' , Branch ='" & branch_txt.Text & "' , Class = '" & class_txt.Text & "' where id=" & id_txt.Text & ""
Dim cmd As New OleDbCommand(access, conaccess)
cmd.ExecuteNonQuery()
loadGrid()
id_txt.Clear()
name_txt.Clear()
branch_txt.Clear()
age_txt.Clear()
class_txt.Clear()
gen_txt.Clear()
End Sub
End Class
我做了一个在visual basic 2010和sql server中的项目,我想以另一种形式更新party master中的记录,当我点击更新按钮时,另一个更新表格将打开,并且在dtagrid中将显示数据表的所有记录,在那个表单上有三个按钮,更新,删除,canc el,我只是想在编辑数据行后点击更新按钮,数据保存到数据网格,并且与sql server中的数据表相同。
更新表格代码如下。
Imports System.Data.SqlClient
进口系统
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms.DataGridView
Public Class frmupdate
继承System.Windows.Forms.Form
Dim cnn As New SqlConnection(Data Source = .\SQLEXPRESS; AttachDbFilename = C:\ Users\Admin \ documents \ visual studio 2010 \Projects\VRINDAVAN REALTECH \VRINDAVAN REALTECH \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ >
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim ds As New DataSet()
Private Sub frmupdate_Load(ByVal sender As Object,ByVal e As System.EventArgs)Handles Me.Load
'TODO:这行代码将数据加载到'VrindavanDataSet3 .p_master'表。您可以根据需要移动或移除它。
Me.P_masterTableAdapter.Fill(Me.VrindavanDataSet3.p_master)
Dim sql As String
尝试
cnn.Open()
Dim da As SqlDataAdapter
Dim ds As New DataSet()
sql =从p_master中选择*;
da =新的SqlDataAdapter(sql,cnn)
da.Fill(ds,p_master )
DataGridView1.DataSource = ds
DataGridView1.DataMember =p_master
DataGridView1.DataSource = ds.DefaultViewManager
cnn.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical,Me.Text)
结束尝试
结束子
Private Sub btnupdate_Click(ByVal sender As System.Object,ByVal e As Syst em.EventArgs)处理btnupdate.Click
ds.Tables(p_master)。AcceptChanges()
da.Update(ds,p_master)
Dim item As New DataGridViewRow
DataGridView1.AllowUserToAddRows = True
item.CreateCells(DataGridView1)
MessageBox.Show(Record更新成功)
End Sub
End Class
请给我解决方案,这非常紧急。
I have made a project in visual basic 2010 and sql server, i want to update the record in party master in another form , where when i click on the update button the another update form will open and there in the dtagrid all the records of data table will present, on that form there is three button , update, delete, cancel, i just want that when i click on the update button after editing the data row the data save to the datagrid and same to data table in sql server.
the update form code is below.
Imports System.Data.SqlClient
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms.DataGridView
Public Class frmupdate
Inherits System.Windows.Forms.Form
Dim cnn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\documents\visual studio 2010\Projects\VRINDAVAN REALTECH\VRINDAVAN REALTECH\vrindavan.mdf;Integrated Security=True;User Instance=True ")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim da As SqlDataAdapter
Dim ds As New DataSet()
Private Sub frmupdate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'TODO: This line of code loads data into the 'VrindavanDataSet3.p_master' table. You can move, or remove it, as needed.
Me.P_masterTableAdapter.Fill(Me.VrindavanDataSet3.p_master)
Dim sql As String
Try
cnn.Open()
Dim da As SqlDataAdapter
Dim ds As New DataSet()
sql = "Select * from p_master;"
da = New SqlDataAdapter(sql, cnn)
da.Fill(ds, "p_master")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "p_master"
DataGridView1.DataSource = ds.DefaultViewManager
cnn.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
End Try
End Sub
Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
ds.Tables("p_master").AcceptChanges()
da.Update(ds, "p_master")
Dim item As New DataGridViewRow
DataGridView1.AllowUserToAddRows = True
item.CreateCells(DataGridView1)
MessageBox.Show("Record updated successfully")
End Sub
End Class
Please give me the solution, it's very urgent.
这篇关于VB.net:如何从datagridview更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文