|
 |
|
ถ้าต้องการเขียนโค้ดเช็ค นามสกุล ผมต้องเพิ่มตัวไหนลงไปบรรทัดไหนอ่ะคับ รบกวนหน่อย |
|
 |
|
|
 |
 |
|
Code (C#)
protected void btnRegister_Click(object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
String strConnString, strSQL;
//StringBuilder strSQL =default(StringBuilder);
int intCount = 0;
//*** Open Connection ***'
strConnString = "Data Source=NEWPROGRAMMER;Initial Catalog=customer;Integrated Security=True";
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
strSQL = "INSERT INTO register (username,lastname,password,address,tel,mobile,email) " +
" VALUES " +
" ('" + this.txtUsername.Text + "','" + this.txtLastname.Text + "','"+ EncMD5("'" + this.txtPassword.Text + "'") +"', " +
" '" + this.txtAddress.Text + "','" + this.txtTel.Text + "','" + this.txtMobile.Text + "','" + this.txtEmail.Text +"' )";
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
this.pnlRegister.Visible = false;
try
{
objCmd.ExecuteNonQuery();
this.lblStatus.Text = "Record Inserted";
this.lblStatus.Visible = true;
//เพิ่ม
// Response.Redirect("index.aspx");
}
catch (Exception ex)
{
this.lblStatus.Visible = true;
this.lblStatus.Text = "Record can not insert Error (" + ex.Message + ")";
}
objConn.Close();
objConn = null;
this.pnlFinish.Visible = true;
Response.Write("<Script language=javascript> alert('คุณทำการ Register เรียบร้อยแล้วรอ Approve จากผู้ดูแลระบบ');window.location = 'index.aspx';</Script>");
//** Check Lastname **//
strSQL = "SELECT COUNT (*) FROM register WHERE lastname = '" + lblLastname.Text + "' ";
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
if (intCount >=1)
{
this.lblLastname.Text = "Lastname already exists!!";
return;
}
//**End Check Lastname **//
objConn.Close();
objConn = null;
}
มันขึ้น ExecuteNonQuery: Connection property has not been initialized. อ่ะครับตครงเช็ค Lastname
ขอบคุณคับ
และผมอยากทราบว่าการเขียน 2 แบบนี้ต่างกันยังไงอ่ะครับ
Code (C#)
strSQL = "SELECT COUNT (*) FROM register WHERE lastname = '" + lblLastname.Text + "' ";
objCmd = new SqlCommand(strSQL, objConn);
Code (C#)
strSQL = new StringBuilder();
strSQL.Append(" SELECT COUNT(*) FROM member ");
strSQL.Append(" WHERE Username = @sUsername ");
objCmd = new SqlCommand(strSQL.ToString(), objConn);
objCmd.Parameters.Add("@sUsername", SqlDbType.VarChar).Value = this.txtUsername.Text;
intCount = (int)objCmd.ExecuteScalar();
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#, VS 2010 (.NET 4.x)

|
|
 |
 |
 |
 |
Date :
2013-04-30 10:18:51 |
By :
offonepoint |
View :
895 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
บรรทัดที่ 50-52 select ข้อมูลแต่ใช้ executenonquery
ส่วนคำถามที่ 2 ตอบว่าได้ผลไม่ต่างกัน
แต่แบบแรก ถ้า command ยาวๆ จะลำบากตอน debug แต่เสี่ยงต่อการถูก sql injection เพราะใช้การต่อ string
แนะนำให้เขียนแบบที่สอง ทั้งการใช้ stringbuilder และการ assign ค่าแบบ parameter
|
 |
 |
 |
 |
Date :
2013-04-30 10:36:00 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเปลี่ยนเป็น ExecuteScalar ดูครับ
|
 |
 |
 |
 |
Date :
2013-04-30 14:18:27 |
By :
CPU4Core |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|