|
|
|
สอบถามเรื่องการ insert ข้อมูล 2 ตารางครับ เกิดปัญหา insert แค่บางส่วนครับ |
|
|
|
|
|
|
|
จากโค้ด insert ข้อมูลอ่ะครับ พอจะแนะนำวิธีไหนบ้างที่จะให้ insert คำสั่งด้านบนก่อนอ่ะครับ
เพราะตอนนี้ insert เฉพาะโค็ด insert ด้านล่างอย่างเดียว ลองทดสอบโค็ดด้านบนแล้วใช้ได้ครับ แต่พอรวมกันจะ insert เฉพาะอันล่างครับ
ขอบคุณล่วงหน้าครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Security;
using System.Text.RegularExpressions;
using System.IO;
namespace ClassRoomManagementSystem
{
public partial class manual : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["LOGIN"] == null)
{
Response.Redirect("login.aspx");
}
else
{
lblLOGIN.Text = Session["LOGIN"].ToString();
if (Convert.ToInt32(Session["ACCESS"]) == 01)
{
pnADMIN.Visible = true;
pnUSER.Visible = false;
}
else
{
pnADMIN.Visible = false;
pnUSER.Visible = true;
}
}
PopulateYearList();
}
protected void PopulateYearList()
{
ddlyear.Items.Clear();
int year;
for (year = DateTime.Now.Year; year <= DateTime.Now.Year + 5; year++)
{
ddlyear.Items.Add(System.Convert.ToString(year + 543));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
String url = null;
string strConnString, strSQL;
string condition = "9";
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlConnection myconn = new SqlConnection("Server=WPEE-PC\\SA;
UID=sa;PASSWORD=1q2w3e4r;DATABASE=dbCRMS;MAX Pool Size=400; Connect Timeout=600;");
String username;
String sqlStr = "select top 1 COURSE_ID from tblCOURSE order by COURSE_ID DESC";
SqlCommand myCmd = new SqlCommand(sqlStr, myconn);
myconn.Open();
object maxID = myCmd.ExecuteScalar();
myconn.Close();
username = maxID.ToString();
strConnString = "Server=WPEE-PC\\SA; UID=sa;PASSWORD=1q2w3e4r;DATABASE=dbCRMS;MAX Pool Size=400; Connect Timeout=600;";
strSQL = "Insert Into tblCOURSE (COURSE_CODE,COURSE_NAME,TEACHER,CREDIT,LEC,QUANTITY,COURSE_YEAR,COURSE_TERM,CONDITION) " +
" values ('" + txtcourse_code.Text + "','" + txtcourse_name.Text + "','" + txtteacher.Text + "','" + txtcredit.Text + "','" + txtlec.Text + "','" + txtquantity.Text + "','" + ddlyear.Text + "','" + ddlterm.Text + "','" + condition + "')";
strSQL = "Insert Into tblRESERV_ROOM (RESERV_ROOM,RESERV_DAY,USER_NAME) " +
" values ('" + ddlROOM_NAME.Text + "','" + DropDownList1.Text + "','" + username + "')";
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objCmd.ExecuteNonQuery();
objConn.Close();
objConn = null;
url = "reserv-management.aspx";
Response.Redirect(url);
}
}
}
Tag : ASP.NET, MySQL, C#
|
|
|
|
|
|
Date :
2011-10-14 00:39:00 |
By :
wpeeIT |
View :
1236 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำไมคุณจะต้อง Open ทั้ง 2 ตัวครับ และ Connection String ก็ไม่เหมือนกันอีก ลองเช็คดูดี ๆ ครับ
ลองแบบนี้ได้เลยครับ
Code (C#)
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
String strConnString,strSQL;
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
objConn.ConnectionString = strConnString;
objConn.Open();
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES " +
" ('" + this.txtCustomerID.Text + "','" + this.txtName.Text + "','" + this.txtEmail.Text + "', " +
" '" + this.txtCountryCode.Text + "','" + this.txtBudget.Text + "','" + this.txtUsed.Text + "')";
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objCmd.ExecuteNonQuery();
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES " +
" ('" + this.txtCustomerID.Text + "','" + this.txtName.Text + "','" + this.txtEmail.Text + "', " +
" '" + this.txtCountryCode.Text + "','" + this.txtBudget.Text + "','" + this.txtUsed.Text + "')";
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objCmd.ExecuteNonQuery();
objConn.Close();
objConn = null;
|
|
|
|
|
Date :
2011-10-14 09:29:35 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|