从文本框中添加,更新和删除(访问) [英] Add, Update And Delete From A Textbox (Access)
问题描述
那里.
请有人解释不使用绑定导航"控件向访问数据库添加,更新和删除记录的简化列表和有效方法.由于绑定导航不包含任何有关如何添加/更新数据集的代码.我已经搜索了谷歌,我发现的全部是如何直接插入数据库,但是我的应用程序使用了数据绑定控件.请有人指出正确的方向....您可以告诉我一个菜鸟.
我有一个包含1个表的数据库,该表称为Field_Items,具有字段ID(0),Name(1),Description(2).我有一个表单,其中包含3个文本框以匹配表中的字段,并具有2个用于执行新操作和保存操作的按钮.
Hi There.
Please could someone explain the simplist and effective method to Add, Update And Delete records to a access database without using the Bind Navigation control. As the Bind navigation does not contain any code to how it adds/updates the dataset. I have searched google and all i find is how to insert directly into the database but my application uses databound controls. Please could someone point in the right direction....as you can tell im a noobist.
I have a database with 1 table called Field_Items with the fields ID(0), Name(1), Description(2). I have a form with 3 textbox to match the fields in the table and 2 buttons for new and save action.
Thanks in Advance!
推荐答案
首先导入以下dll''s
First Import the following dll''s
Imports System.Data
Imports System.Data.OleDb
在应用程序和数据库之间创建连接
To create connection between application and Database
Public Sub OpenConnection()
If con.State = ConnectionState.Open Then
con.Close()
End If
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\DatabaseName.mdb"
Try
con.Open()
Catch ex As Exception objda.SelectCommand = New OleDbCommand
objda.UpdateCommand = New OleDbCommand
objda.SelectCommand.Connection = con objda.UpdateCommand.Connection = con
End Sub
添加新数据只不过是清除文本框"字段
Adding New Data is nothing but the clearing the Textbox fields
Texbox1.Clear
Texbox2.Clear
要将Texbox字段中的数据保存到Access Daabase,
To Save data form Texbox fields to Access Daabase
objda.SelectCommand.CommandText = "Insert into TableName(Field1, Field2) values(@Field1, @Field2)"
objda.SelectCommand.Parameters.AddWithValue("@StudID", txtstudid.Text)
Try
OpenConnection()
objda.SelectCommand.ExecuteNonQuery()
MessageBox.Show("Information saved Successfully", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message)
End Try
更新数据库中存在的值
To Upadate Values present into the Database
objda.UpdateCommand.CommandText = "Update TableName SET Field1='" & txtstudname.Text & "', Field2='" & txtmother.Text & "'"
Try
OpenConnection()
objda.UpdateCommand.ExecuteNonQuery()
MessageBox.Show("Information Modified Successfully", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message)
End Try
从Detabase删除记录
To Delete the record from Detabase
objda.SelectCommand.CommandText = "DELETE * FROM TableName WHERE Field1='" & Textbox1.Text & "'"
Try
OpenConnection()
objda.SelectCommand.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
如果可以,请尝试此操作.调试以跟踪代码流...
只需在后面的 代码 ...
Hi,
Try this if could help. Debug to trace the flow of code...
Just separate code behind...
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript" language="javascript">
function DeleteData() {
return confirm("Are you sure you want to delete the record");
}
</script>
<script runat="server">
Public recordPointer As Int32 = 0
Public Class Attachments
Private _id As Int32
Private _pageName As String
Public Property Id() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property
Public Property PageName() As String
Get
Return _pageName
End Get
Set(ByVal value As String)
_pageName = value
End Set
End Property
Public Sub Attachments()
End Sub
End Class
Protected Sub TaskGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs)
End Sub
Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.addNew.Enabled = False
Me.addNew.Visible = False
Me.submit.Enabled = True
Me.submit.Visible = True
End Sub
Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs)
BindData(recordPointer)
End Sub
Protected Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
recordPointer = 0
BindData(recordPointer)
End Sub
Protected Sub BindData(ByVal seclectedRecord As Int32)
Dim lstAttchments As List(Of Attachments) = New List(Of Attachments)
Dim strSQL2 As String = "SELECT ID, pagename FROM tblPages"
Dim dbsource As String = "acm2000.mdb"
Dim iMapPath As String = Server.MapPath(".").ToString() & "\app_data\"
Dim iConn2 As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & iMapPath & dbsource)
Dim iCmd2 As New OleDbCommand(strSQL2, iConn2)
iConn2.Open()
Using (iCmd2)
iCmd2.CommandType = CommandType.Text
Dim dr As OleDbDataReader = iCmd2.ExecuteReader()
Using (dr)
While (dr.Read())
Dim temp As Attachments = New Attachments()
temp.Id = dr("id")
temp.PageName = dr("pageName")
lstAttchments.Add(temp)
End While
End Using
End Using
If lstAttchments.Count > 0 And recordPointer = 0 Then
End If
Dim lst As Object = lstAttchments
GridView1.DataSource = lstAttchments
GridView1.DataBind()
iConn2.Close()
iCmd2.Dispose()
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
Public Sub GridView1_LnkDelete(ByVal Sender As Object, ByVal e As EventArgs)
Dim LnkDelete As LinkButton = DirectCast(Sender, LinkButton)
Dim row As GridViewRow = DirectCast(LnkDelete.NamingContainer, GridViewRow)
Dim strId As String = row.Cells(2).Text
Dim strPageName As String = row.Cells.Item(3).Text
'todo Delete code here...
End Sub
Public Sub GridView1_LnkEdit(ByVal Sender As Object, ByVal e As EventArgs)
Dim LnkEdit As LinkButton = DirectCast(Sender, LinkButton)
Dim row As GridViewRow = DirectCast(LnkEdit.NamingContainer, GridViewRow)
Dim strId As String = row.Cells(2).Text
Dim strPageName As String = row.Cells.Item(3).Text
'todo Edit code here...
End Sub
Protected Sub addNew_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'todo Your add code here...
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="font-family: Arial, Helvetica, sans-serif;">
<form id="Form1" runat="server" onload="Form1_Load">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:Button ID="addNew" runat="server" Text="Add New" OnClick="addNew_Click" />
<asp:Button ID="submit" runat="server" Text="Save page contents" OnClick="submit_Click"
Visible="false" />
<br />
<table width="100%">
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
OnRowUpdated="TaskGridView_RowUpdated" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
OnPageIndexChanging="GridView1_PageIndexChanging">
<columns>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<itemtemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("ID") %>'
OnCommand="GridView1_LnkDelete" OnClientClick="return DeleteData();"> Delete
</itemtemplate>
<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<itemtemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%#Eval("ID") %>' OnCommand="GridView1_LnkEdit"> Edit
</itemtemplate>
<asp:BoundField DataField="ID" HeaderText="ID No." InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="pagename" HeaderText="Page Name" SortExpression="pagename" />
</columns>
<asp:HiddenField ID="hfId" runat="server" />
<asp:HiddenField ID="hfPageName" runat="server" />
<br />
</td>
</tr>
</table>
</form>
</body>
</html>
如果可以帮助的话,别忘了投票...
问候,
Al Moje
Do not forget to vote if could help...
Regrds,
Al Moje
这篇关于从文本框中添加,更新和删除(访问)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!