(C#) ASP.NET MySQL Add-Insert Multiple Record |
(C#) ASP.NET MySQL Add-Insert Multiple Record การเพิ่มข้อมูลลงในฐานข้อมูลด้วย ASP.NET กับ MySQL โดยในตัวอย่างมีการนำ GridView สร้างจำนวนแถว และสามารถทำการเพิ่มได้หลาย Record
Instance NameSpace
C#Using System.Data;
Using MySql.Data.MySqlClient;
ASP.NET & MySql.Data.MySqlClient
Language Code : VB.NET || C#
AspNetMySQLMultiAddInsert.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="MySql.Data.MySqlClient"%>
<%@ Page Language="C#" %>
<script runat="server">
MySqlConnection objConn;
MySqlCommand objCmd;
string strSQL;
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
ShowGridViewRows();
}
}
public void ShowGridViewRows()
{
string[] myRows = new string[4];
myGridView.DataSource = myRows;
myGridView.DataBind();
}
public void Button1_Click(object sender, EventArgs e)
{
CheckBox chkCusID;
int i = 0;
MySqlConnection objConn = new MySqlConnection();
MySqlCommand objCmd = new MySqlCommand();
string strConnString = null;
string strSQL = null;
TextBox strCustomerID;
TextBox strName;
TextBox strEmail;
TextBox strCountryCode;
TextBox strBudget;
TextBox strUsed;
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false";
objConn.ConnectionString = strConnString;
objConn.Open();
this.lblStatus.Text = "";
for (i = 0; i <= myGridView.Rows.Count - 1; i++) {
strCustomerID = (TextBox)myGridView.Rows[i].FindControl("txtAddCustomerID");
strName = (TextBox)myGridView.Rows[i].FindControl("txtAddName");
strEmail = (TextBox)myGridView.Rows[i].FindControl("txtAddEmail");
strCountryCode = (TextBox)myGridView.Rows[i].FindControl("txtAddCountryCode");
strBudget = (TextBox)myGridView.Rows[i].FindControl("txtAddBudget");
strUsed = (TextBox)myGridView.Rows[i].FindControl("txtAddUsed");
//*** IF Not Empty Value ***'
if (strCustomerID.Text.ToString() != "" && strEmail.Text.ToString() != "" &&
strCountryCode.Text.ToString() != "" && strBudget.Text.ToString() != "" && strUsed.Text.ToString() != "") {
//*** 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 + "')";
{
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
}
this.pnlAdd.Visible = false;
try {
objCmd.ExecuteNonQuery();
this.lblStatus.Text = "Record Insert Sucessful.";
this.lblStatus.Visible = true;
} catch (Exception ex) {
this.lblStatus.Visible = true;
this.lblStatus.Text = "Record Cannot Insert : Error (" + strSQL + ")";
}
}
}
objConn.Close();
objConn = null;
}
</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 MySql.Data.MySqlClient - Parameter Query
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2010-07-04 13:35:16 /
2017-03-29 10:09:08 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|