|
 |
|
ขอสอบถามเรื่อง C# Insert ข้อความ เข้าสู่ sql server DB ครับ |
|
 |
|
|
 |
 |
|
Code (C#)
private void save_Click(object sender, EventArgs e)
{
string connetionString = null;
string dt1 = dateTimePickerStart.Value.Year + "/" + dateTimePickerStart.Value.Month + "/" + dateTimePickerStart.Value.Day;
string dt2 = dateTimePickerFinish.Value.Year + "/" + dateTimePickerFinish.Value.Month + "/" + dateTimePickerFinish.Value.Day;
string dt3 = dateTimePickerThisday.Value.Year + "/" + dateTimePickerThisday.Value.Month + "/" + dateTimePickerThisday.Value.Day;
SqlConnection cnn;
connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB.mdf;Integrated Security=True;User Instance=True";
cnn = new SqlConnection(connetionString);
// try
// {
cnn.Open();
//string sql = @"INSERT INTO PRB_TB(ID,pname,fname,lname,nickname,address,tambon,ampher,province,postcode,hometel,mobiletel,email,note1,carname,carid,motorid,cartype,summoney,start,finish,thisday,company,note2) VALUES('" + prbid.Text + "','" + pname.Text + "','" + firstname.Text + "','" + lastname.Text + "','" + nickname.Text + "','" + address.Text + "','" + tambon.Text + "','" + ampher.Text + "','" + province.Text + "','" + postcode.Text + "','" + hometel.Text + "','" + mobile.Text + "','" + email.Text + "','" + note1.Text + "','" + carname.Text + "','" + carid.Text + "','" + motorid.Text + "','" + cartype.Text + "','" + summoney.Text + "','" + dt1 + "','" + dt2 + "','" + dt3 + "','" + company.Text + "','" + note2.Text + "')";
string sql = @"INSERT INTO PRB_TB(ID,pname,fname,lname,nickname,address,tambon,ampher,province,postcode,hometel,mobiletel,email,note1,carname,carid,motorid,cartype,summoney,start,finish,thisday,company,note2) " + " VALUES " + "('" + this.prbid.Text + "','" + this.pname.Text + "','" + this.firstname.Text + "','" + this.lastname.Text + "','" + this.nickname.Text + "','" + this.address.Text + "','" + this.tambon.Text + "','" + this.ampher.Text + "','" + this.province.Text + "','" + this.postcode.Text + "','" + this.hometel.Text + "','" + this.mobile.Text + "','" + this.email.Text + "','" + this.note1.Text + "','" + this.carname.Text + "','" + this.carid.Text + "','" + this.motorid.Text + "','" + this.cartype.Text + "','" + this.summoney.Text + "','" + dt1 + "','" + dt2 + "','" + dt3 + "','" + this.company.Text + "','" + this.note2.Text + "')";
//string sql = "INSERT INTO PRB_TB(pname,fname,lname,nickname,address,tambon,ampher,province,postcode,hometel,mobiletel,email,note1) " + " VALUES " + "('" + this.pname + "','" + firstname + "','" + lastname + "','" + nickname + "','" + address + "','" + tambon + "','" + ampher + "','" + province + "','" + postcode + "','" + hometel + "','" + mobile + "','" + email + "','" + note1 + "')";
SqlCommand cmd = new SqlCommand(sql, cnn);
cmd.ExecuteNonQuery();//คิวรี่ข้อมูล
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว");
cnn.Close();
// }
// catch (Exception ex)
// {
// MessageBox.Show("ไม่สามารถบันทึกข้อมูลได้");
//}
}
คือผมแอดเข้า Data service .mdf ใน visual studio 2010 ไม่ได้ครับ รบกวนพี่ๆหน่อยครับ ผมผิดตรงซินแทคไหนเหรอครับ รบกวนจิงๆครับ
อันนี้ที่ผมแอดครับ
Code (C#)
private void save_Click(object sender, EventArgs e)
{
string connetionString = null;
string dt1 = dateTimePickerStart.Value.Year + "/" + dateTimePickerStart.Value.Month + "/" + dateTimePickerStart.Value.Day;
string dt2 = dateTimePickerFinish.Value.Year + "/" + dateTimePickerFinish.Value.Month + "/" + dateTimePickerFinish.Value.Day;
string dt3 = dateTimePickerThisday.Value.Year + "/" + dateTimePickerThisday.Value.Month + "/" + dateTimePickerThisday.Value.Day;
SqlConnection cnn;
connetionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB.mdf;Integrated Security=True;User Instance=True";
cnn = new SqlConnection(connetionString);
cnn.Open();
string sql = @"INSERT INTO PRB_TB(ID,pname,fname,lname,nickname,address,tambon,ampher,province,postcode,hometel,mobiletel,email,note1,carname,carid,motorid,cartype,summoney,start,finish,thisday,company,note2) " + " VALUES " + "('" + this.prbid.Text + "','" + this.pname.Text + "','" + this.firstname.Text + "','" + this.lastname.Text + "','" + this.nickname.Text + "','" + this.address.Text + "','" + this.tambon.Text + "','" + this.ampher.Text + "','" + this.province.Text + "','" + this.postcode.Text + "','" + this.hometel.Text + "','" + this.mobile.Text + "','" + this.email.Text + "','" + this.note1.Text + "','" + this.carname.Text + "','" + this.carid.Text + "','" + this.motorid.Text + "','" + this.cartype.Text + "','" + this.summoney.Text + "','" + dt1 + "','" + dt2 + "','" + dt3 + "','" + this.company.Text + "','" + this.note2.Text + "')";
SqlCommand cmd = new SqlCommand(sql, cnn);
cmd.ExecuteNonQuery();//คิวรี่ข้อมูล
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว");
cnn.Close();
}
Tag : .NET

|
ประวัติการแก้ไข 2013-03-18 11:36:26 2013-03-18 14:05:41
|
 |
 |
 |
 |
Date :
2013-03-18 11:34:16 |
By :
clashhum |
View :
1029 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Insert ไม่ได้มันน่าจะมี Error Message มาให้ครับ
|
 |
 |
 |
 |
Date :
2013-03-18 16:16:27 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|