|
|
|
C# คือต้องการเช็คค่าข้อมูลที่เซฟ ไม่ให้ซ้ำกับข้อมูลในดาตาเบสคับ |
|
|
|
|
|
|
|
Code (C#)
private void cmdSave_Click(object sender, EventArgs e)
{
if (MessageBox.Show("คุณต้องการบันทึกใช่หรือไม่", "ยืนยันการทำงาน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
tr = Conn.BeginTransaction();
string CHID;
string CName;
string CWID;
string CTel;
string CStrart;
string CWorkage;
string CTimeIn;
string CTimeOut;
string sqlSave;
try
{
for (int i = 0; i <= dgvHumanList.Rows.Count - 1; i++)
{
if (dgvHumanList.Rows[i].Cells["ColIn"].Value != null)
{
CHID = dgvHumanList.Rows[i].Cells["ColHID"].Value.ToString();
CName = dgvHumanList.Rows[i].Cells["ColName"].Value.ToString();
CWID = dgvHumanList.Rows[i].Cells["ColPosition"].Value.ToString();
CTel = dgvHumanList.Rows[i].Cells["ColTel"].Value.ToString();
CStrart = dgvHumanList.Rows[i].Cells["ColStart"].Value.ToString();
CWorkage = dgvHumanList.Rows[i].Cells["ColWorkAge"].Value.ToString();
CTimeIn = dgvHumanList.Rows[i].Cells["ColIn"].Value.ToString();
CTimeOut = dgvHumanList.Rows[i].Cells["ColOut"].Value.ToString();
// MessageBox.Show(CWID);
// sb = new StringBuilder();
sb.Remove(0, sb.Length);
sb.Append("INSERT INTO Humanschedule(Date,HID,WorktypeName,Time_IN,Time_OUT)");
sb.Append(" VALUES(@Date,@HID,@WorktypeName,@Time_IN,@Time_OUT)");
sqlSave = sb.ToString();
com.Connection = Conn;
com.CommandText = sqlSave;
com.Transaction = tr;
com.Parameters.Clear();
com.Parameters.Add("Date", SqlDbType.DateTime).Value = datework.Value;
com.Parameters.Add("HID", SqlDbType.NVarChar).Value = CHID;
com.Parameters.Add("WorktypeName", SqlDbType.NVarChar).Value = CWID;
com.Parameters.Add("Time_IN", SqlDbType.NVarChar).Value = CTimeIn;
com.Parameters.Add("Time_OUT", SqlDbType.NVarChar).Value = CTimeOut;
com.ExecuteNonQuery();
}
}
tr.Commit();
MessageBox.Show("บันทึกเรียบร้อย", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
tr.Rollback();
MessageBox.Show("คุณได้กำหนดตารางงานของวันนี้ไปแล้ว", "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
คือต้องการเงื่อนไข ว่า ใน DATE เดียวกัน HID จะเซฟซ้ำอีกไม่ได้
Tag : .NET, C#, VS 2005 (.NET 2.x)
|
ประวัติการแก้ไข 2012-02-24 02:29:51
|
|
|
|
|
Date :
2012-02-24 02:26:35 |
By :
maxxana |
View :
1728 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คุณจะต้องเขียนคำสั่งเพื่อไปตรวจสอบใน Table ก่อนครับ
Code (C#)
int intNumRows;
strSQL = "SELECT COUNT(*) FROM customer WHERE CustomerID = '"+ this.txtCustomerID.Text +"' ";
objCmd = new OleDbCommand(strSQL, objConn);
intNumRows = Convert.ToInt32(objCmd.ExecuteScalar());
if(intNumRows > 0)
{
this.pnlAdd.Visible = false;
this.lblStatus.Visible = true;
this.lblStatus.Text = "CustomerID already exists.";
}
else
{
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 = new OleDbCommand();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
this.pnlAdd.Visible = false;
try
{
objCmd.ExecuteNonQuery();
this.lblStatus.Text = "Record Inserted";
this.lblStatus.Visible = true;
}
catch (Exception ex)
{
this.lblStatus.Visible = true;
this.lblStatus.Text = "Record can not insert Error ("+ ex.Message +")";
}
}
|
|
|
|
|
Date :
2012-02-24 06:22:42 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|