ASP.NET SQL Server Add-Insert Multiple Record |
ASP.NET SQL Server Add-Insert Multiple Record การเพิ่มข้อมูลลงในฐานข้อมูลด้วย ASP.NET กับ Microsoft SQL Server โดยในตัวอย่างมีการนำ GridView สร้างจำนวนแถว และสามารถทำการเพิ่มได้หลาย Record
Instance NameSpace
VB.NETImports System.Data
Imports System.Data.SqlClient
ASP.NET & System.Data.SqlClient
Language Code : VB.NET || C#
AspNetSQLServerMultiAddInsert.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Sub Page_Load(sender As Object, e As EventArgs)
IF Not Page.IsPostBack() Then
ShowGridViewRows()
End IF
End Sub
Sub ShowGridViewRows()
Dim myRows(4)
myGridView.DataSource = myRows
myGridView.DataBind()
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
Dim chkCusID As CheckBox
Dim i As Integer
Dim objConn As New SqlConnection
Dim objCmd As New SqlCommand
Dim strConnString,strSQL As String
Dim strCustomerID As TextBox
Dim strName As TextBox
Dim strEmail As TextBox
Dim strCountryCode As TextBox
Dim strBudget As TextBox
Dim strUsed As TextBox
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn.ConnectionString = strConnString
objConn.Open()
Me.lblStatus.Text = ""
For i = 0 To myGridView.Rows.Count - 1
strCustomerID = CType(myGridView.Rows(i).FindControl("txtAddCustomerID"),TextBox)
strName = CType(myGridView.Rows(i).FindControl("txtAddName"),TextBox)
strEmail = CType(myGridView.Rows(i).FindControl("txtAddEmail"),TextBox)
strCountryCode = CType(myGridView.Rows(i).FindControl("txtAddCountryCode"),TextBox)
strBudget = CType(myGridView.Rows(i).FindControl("txtAddBudget"),TextBox)
strUsed = CType(myGridView.Rows(i).FindControl("txtAddUsed"),TextBox)
'*** IF Not Empty Value ***'
IF Trim(strCustomerID.Text) <> "" _
AND Trim(strName.Text) <> "" _
AND Trim(strEmail.Text) <> "" _
AND Trim(strCountryCode.Text) <> "" _
AND Trim(strBudget.Text) <> "" _
AND Trim(strUsed.Text) <> "" Then
'*** Insert Statement ***'
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES " & _
" ('" & strCustomerID.Text & "','" & strName.Text & "','" & strEmail.Text & "', " & _
" '" & strCountryCode.Text & "','" & strBudget.Text & "','" & strUsed.Text & "')"
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Me.pnlAdd.Visible = False
Try
objCmd.ExecuteNonQuery()
Me.lblStatus.Text = "Record Insert Sucessful."
Me.lblStatus.Visible = True
Catch ex As Exception
Me.lblStatus.Visible = True
Me.lblStatus.Text = "Record Cannot Insert : Error ("& ex.Message &")"
End Try
End IF
Next
objConn.Close()
objConn = Nothing
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel id="pnlAdd" runat="server">
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<asp:TextBox id="txtAddCustomerID" size="5" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox id="txtAddName" size="10" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox id="txtAddEmail" size="20" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryCode">
<ItemTemplate>
<asp:TextBox id="txtAddCountryCode" size="2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:TextBox id="txtAddBudget" size="6" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:TextBox id="txtAddUsed" size="6" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>
<hr />
</asp:Panel>
<asp:Label id="lblStatus" runat="server" visible="False"></asp:Label>
</form>
</body>
</html>
Screenshot
ASP.NET System.Data.SqlClient - Parameter Query
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2010-07-04 13:37:01 /
2017-03-29 10:29:40 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|