วิธีเอาข้อมูล dateTimePicker1 ลงใน MySql ทำยังงัยบ้างครับ ผมทำหลายวิธีแล้วยังไม่ได้เลย แก้ยังงัยมันกะยังเป็นค่า Null อยุ่ครับ C# น่ะครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace DEMO
{
public partial class FormSave_1_3 : Form
{
MySqlConnection objConn = new MySqlConnection();
MySqlCommand objCmd = new MySqlCommand();
public FormSave_1_3()
{
InitializeComponent();
}
private void home_Click(object sender, EventArgs e)
{
this.Hide();
FormHome f = new FormHome();
f.Show();
}
private void personal_Click(object sender, EventArgs e)
{
this.Hide();
FormReport f = new FormReport();
f.Show();
}
private void employee_Click(object sender, EventArgs e)
{
this.Hide();
FormSave f = new FormSave();
f.Show();
}
private void hometax_Click(object sender, EventArgs e)
{
this.Hide();
FormSystem f = new FormSystem();
f.Show();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure to exit?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
Application.Exit();
}
}
private void นกเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_1_1 f = new FormReport_1_1();
f.Show();
}
private void บญชรายชอนกเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_1_2 f = new FormReport_1_2();
f.Show();
}
private void ครToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_1_3 f = new FormReport_1_3();
f.Show();
}
private void บญชรายชอครToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_1_4 f = new FormReport_1_4();
f.Show();
}
private void อนมตจบToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_1_5 f = new FormReport_1_5();
f.Show();
}
private void ตรวจสอบจบToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_1_6 f = new FormReport_1_6();
f.Show();
}
private void ทะเบยนเรยนรายหองToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormRePort_2_1 f = new FormRePort_2_1();
f.Show();
}
private void ทะเบยนเรยนรายบคคลToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormRePort_2_2 f = new FormRePort_2_2();
f.Show();
}
private void ทToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormRePort_2_3 f = new FormRePort_2_3();
f.Show();
}
private void ทะเบยนเรยนรายผสอนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormRePort_2_4 f = new FormRePort_2_4();
f.Show();
}
private void วชาเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormRePort_2_5 f = new FormRePort_2_5();
f.Show();
}
private void แผนการเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormRePort_2_6 f = new FormRePort_2_6();
f.Show();
}
private void ใบแจงผลการเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_1 f = new FormReport_3_1();
f.Show();
}
private void แบบประกาศผลการเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_2 f = new FormReport_3_2();
f.Show();
}
private void รายชอผทตด0รมสรายชนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_3 f = new FormReport_3_3();
f.Show();
}
private void รายชอผทตด0รมสรายวชาToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_4 f = new FormReport_3_4();
f.Show();
}
private void รายชอผทตด0รมสในชวงปToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_5 f = new FormReport_3_5();
f.Show();
}
private void รายชอผทไมผานกจกรรมToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_6 f = new FormReport_3_6();
f.Show();
}
private void ประเมนคณToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_7 f = new FormReport_3_7();
f.Show();
}
private void ใบคำรองขอสอบแกตวToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_8 f = new FormReport_3_8();
f.Show();
}
private void ผลการเรยนรายปToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_9 f = new FormReport_3_9();
f.Show();
}
private void ผลการเรยนตลอดหลกสตรToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_3_10 f = new FormReport_3_10();
f.Show();
}
private void ระเบยนผลการเรยนปพ1ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_4_1 f = new FormReport_4_1();
f.Show();
}
private void ประกาศนยบตรปพ2ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_4_2 f = new FormReport_4_2();
f.Show();
}
private void แบบรายงานผจบการศToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_4_3 f = new FormReport_4_3();
f.Show();
}
private void ใบรบรองผลการศกษาปพ7ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_4_4 f = new FormReport_4_4();
f.Show();
}
private void ตรวจสอบขอมลปพ3ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_4_5 f = new FormReport_4_5();
f.Show();
}
private void สรปผลการเรยนเฉลยGPAToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_5_1 f = new FormReport_5_1();
f.Show();
}
private void สรปผลการเรยนรายชนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_5_2 f = new FormReport_5_2();
f.Show();
}
private void สรปผลการเรยนรายวชาToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_5_3 f = new FormReport_5_3();
f.Show();
}
private void สรปผลการเรยนสงสดตำสดToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_5_4 f = new FormReport_5_4();
f.Show();
}
private void สรปผลการจบหลกสตรToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_5_5 f = new FormReport_5_5();
f.Show();
}
private void จำนวนนกเรยนรายชนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_6_1 f = new FormReport_6_1();
f.Show();
}
private void จำนวนนกเรยนรายอายToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_6_2 f = new FormReport_6_2();
f.Show();
}
private void ผลสมฤทธทางการเรยนรายวชาToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_6_3 f = new FormReport_6_3();
f.Show();
}
private void ผลสมฤทธทางการเรยนรายกลมสาระฯToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormReport_6_4 f = new FormReport_6_4();
f.Show();
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_1_1 f = new FormSave_1_1();
f.Show();
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_1_2 f = new FormSave_1_2();
f.Show();
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_1_3 f = new FormSave_1_3();
f.Show();
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_1_4 f = new FormSave_1_4();
f.Show();
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_1_5 f = new FormSave_1_5();
f.Show();
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_1_6 f = new FormSave_1_6();
f.Show();
}
private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_2_1 f = new FormSave_2_1();
f.Show();
}
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_2_2 f = new FormSave_2_2();
f.Show();
}
private void toolStripMenuItem15_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_2_3 f = new FormSave_2_3();
f.Show();
}
private void toolStripMenuItem16_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_2_4 f = new FormSave_2_4();
f.Show();
}
private void toolStripMenuItem17_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_2_5 f = new FormSave_2_5();
f.Show();
}
private void toolStripMenuItem18_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_2_6 f = new FormSave_2_6();
f.Show();
}
private void toolStripMenuItem20_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_1 f = new FormSave_3_1();
f.Show();
}
private void toolStripMenuItem21_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_2 f = new FormSave_3_2();
f.Show();
}
private void toolStripMenuItem22_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_3 f = new FormSave_3_3();
f.Show();
}
private void toolStripMenuItem23_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_4 f = new FormSave_3_4();
f.Show();
}
private void toolStripMenuItem24_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_5 f = new FormSave_3_5();
f.Show();
}
private void toolStripMenuItem25_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_6 f = new FormSave_3_6();
f.Show();
}
private void toolStripMenuItem26_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_7 f = new FormSave_3_7();
f.Show();
}
private void toolStripMenuItem27_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_8 f = new FormSave_3_8();
f.Show();
}
private void toolStripMenuItem28_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_9 f = new FormSave_3_9();
f.Show();
}
private void toolStripMenuItem29_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_3_10 f = new FormSave_3_10();
f.Show();
}
private void รหสวชาToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_4_1 f = new FormSave_4_1();
f.Show();
}
private void ชอวชาToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_4_2 f = new FormSave_4_2();
f.Show();
}
private void รหสประจำตวToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_4_3 f = new FormSave_4_3();
f.Show();
}
private void ชอนกเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_4_4 f = new FormSave_4_4();
f.Show();
}
private void หองเรยนToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
FormSave_4_5 f = new FormSave_4_5();
f.Show();
}
private void toolStripMenuItem38_Click(object sender, EventArgs e)
{
this.Hide();
FormSystem f = new FormSystem();
f.Show();
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.User_ID.Text))
{
MessageBox.Show("Please input (เลขประจำตัวครู)");
this.User_ID.Focus();
return;
}
if (string.IsNullOrEmpty(this.Pin_ID.Text))
{
MessageBox.Show("Please input (เลขประจำตัวประชาชน)");
this.Pin_ID.Focus();
return;
}
if (string.IsNullOrEmpty(this.Prefix.Text))
{
MessageBox.Show("Please input (คำนำหน้า)");
this.Prefix.Focus();
return;
}
if (string.IsNullOrEmpty(this.T_name.Text))
{
MessageBox.Show("Please input (ชื่อ)");
this.T_name.Focus();
return;
}
if (string.IsNullOrEmpty(this.T_Lastname.Text))
{
MessageBox.Show("Please input (สกุล)");
this.T_Lastname.Focus();
return;
}
if (string.IsNullOrEmpty(this.Sex.Text))
{
MessageBox.Show("Please input (เพศ)");
this.Sex.Focus();
return;
}
if (string.IsNullOrEmpty(this.dateTimePicker1.Text))
{
MessageBox.Show("Please input (วัน/เดือน/ปี เกิด)");
this.dateTimePicker1.Focus();
return;
}
if (string.IsNullOrEmpty(this.Age.Text))
{
MessageBox.Show("Please input (อายุ)");
this.Age.Focus();
return;
}
if (string.IsNullOrEmpty(this.Salary.Text))
{
MessageBox.Show("Please input (เงินเดือน)");
this.Salary.Focus();
return;
}
if (string.IsNullOrEmpty(this.Position.Text))
{
MessageBox.Show("Please input ตำแหน่ง)");
this.Position.Focus();
return;
}
if (string.IsNullOrEmpty(this.Hous_number.Text))
{
MessageBox.Show("Please input (บ้านเลขที่)");
this.Hous_number.Focus();
return;
}
if (string.IsNullOrEmpty(this.Moo.Text))
{
MessageBox.Show("Please input (หมู่ที่)");
this.Moo.Focus();
return;
}
if (string.IsNullOrEmpty(this.Road.Text))
{
MessageBox.Show("Please input (ถนน)");
this.Road.Focus();
return;
}
if (string.IsNullOrEmpty(this.District.Text))
{
MessageBox.Show("Please input (ตำบล)");
this.District.Focus();
return;
}
if (string.IsNullOrEmpty(this.Canton.Text))
{
MessageBox.Show("Please input (อำเภอ)");
this.Canton.Focus();
return;
}
if (string.IsNullOrEmpty(this.Drorince.Text))
{
MessageBox.Show("Please input (จังหวัด)");
this.Drorince.Focus();
return;
}
if (string.IsNullOrEmpty(this.ZIP_code.Text))
{
MessageBox.Show("Please input (รหัสไปรษณีย์)");
this.ZIP_code.Focus();
return;
}
if (string.IsNullOrEmpty(this.Phone_nambers.Text))
{
MessageBox.Show("Please input (เบอร์โทร)");
this.Phone_nambers.Focus();
return;
}
if (string.IsNullOrEmpty(this.E_mail.Text))
{
MessageBox.Show("Please input (E-mail)");
this.E_mail.Focus();
return;
}
String strConnString, strSQL;
strConnString = "Server=localhost;User Id=root; Password=123123; Database=db_project; Pooling=false;Allow Zero Datetime=false;";
objConn = new MySqlConnection(strConnString);
objConn.Open();
strSQL = "INSERT INTO teachers(User_ID,Pin_ID,Prefix,T_name,T_Lastname,Sex,Brithday,Age,Salary,Position,Hous_number,Moo,District,Canton,Drorince,ZIP_code,Phone_nambers,E_mail,Road)" + " VALUES ('" + this.User_ID.Text + "','" + this.Pin_ID.Text + "','" + this.Prefix.Text + "','" + this.T_name.Text + "','" + this.T_Lastname.Text + "','" + this.Sex.Text + "','" + this.Age.Text + "','" + this.dateTimePicker1.Text + "','" + this.Salary.Text + "','" + this.Position.Text + "','" + this.Hous_number.Text + "','" + this.Moo.Text + "','" + this.District.Text + "','" + this.Canton.Text + "','" + this.Drorince.Text + "','" + this.ZIP_code.Text + "','" + this.Phone_nambers.Text + "','" + this.E_mail.Text + "','" + this.Road.Text + "')";
objCmd = new MySqlCommand(strSQL, objConn);
objCmd.ExecuteReader();
MessageBox.Show("Save Successfully.");
}
private void FormSave_1_3_Load(object sender, EventArgs e)
{
}
}
}
Tag : .NET, MySQL, C#, VS 2008 (.NET 3.x)
Date :
2011-07-18 16:43:58
By :
noi014
View :
1572
Reply :
5
อันนี้เป็น vb.net ครับ เขียน Function ไว้ แล้วเรียกใช้ครับ
Code (VB.NET)
Public Function SetDateTime(ByVal DateIn As DateTime) As String
Dim ret As String = ""
If DateIn.Year = 1 Then
ret = "NULL"
ElseIf DateIn.Year > 2500 Then
Dim vYear As String = DateIn.Year - 543
ret = "'" & vYear & "-" & DateIn.ToString("MM-dd HH:mm:ss") & "'"
Else
ret = "'" & DateIn.Year & "-" & DateIn.ToString("MM-dd HH:mm:ss") & "'"
End If
Return ret
End Function
เรียกใช้ก็
Code (VB.NET)
datesend = SetDateTime(Date.Now)
ค่าที่ได้คือ 2011-06-14 16:17:18 อะครับ
ชนิดใน databast เป็น Datetime
Date :
2011-07-18 17:53:47
By :
slurpee55555
อ้อลืมไปครับ ตั้ง dateTimePicker เป็น
Code (VB.NET)
dim dateTime as string=""
dateTime = dateTimePicker1.Value.AddYears(-543).ToString("yyyy-MM-dd")
ค่าที่ได้ คือ 2554-06-14 ครับ
mysql ต้อง format date ให้เป็น 2554-06-14 ครับ
Date :
2011-07-18 17:59:28
By :
slurpee55555
ผมเขียนเปน C#น่ะครับ ทำแบบนี้ได้เลยหรอ
Date :
2011-07-18 18:01:32
By :
noi014
Code (C#)
String strConnString;
strConnString = "Server=localhost;User Id=root; Password=123123; Database=db_project; Pooling=false;Allow Zero Datetime=false;";
objConn = new MySqlConnection(strConnString);
objConn.Open();
//strConnString = "INSERT INTO teachers(User_ID,Pin_ID,Prefix,T_name,T_Lastname,Sex,Brithday,Age,Salary,Position,Hous_number,Moo,District,Canton,Drorince,ZIP_code,Phone_nambers,E_mail,Road)" + " VALUES ('" + this.User_ID.Text + "','" + this.Pin_ID.Text + "','" + this.Prefix.Text + "','" + this.T_name.Text + "','" + this.T_Lastname.Text + "','" + this.Sex.Text + "','" + this.Age.Text + "','" + dateTimePicker1.Value.ToString("yyyy-mm-dd") + "','" + this.Salary.Text + "','" + this.Position.Text + "','" + this.Hous_number.Text + "','" + this.Moo.Text + "','" + this.District.Text + "','" + this.Canton.Text + "','" + this.Drorince.Text + "','" + this.ZIP_code.Text + "','" + this.Phone_nambers.Text + "','" + this.E_mail.Text + "','" + this.Road.Text + "')";
strConnString = "INSERT INTO teachers(Brithday)" + " VALUES ('" + this.dateTimePicker1.Value.ToString() + "')";
objCmd = new MySqlCommand(strConnString, objConn);
objCmd.ExecuteReader();
MessageBox.Show("Save Successfully.");
ยังไม่ได้อะครับมันยัง 0000-00-00 00:00:00 อยุ่เลย
Date :
2011-07-18 18:49:23
By :
noi014
Code (C#)
string dt = dateTimePicker1.Value.ToString("yyyy-MM-dd");
String strConnString;
strConnString = "Server=localhost;User Id=root; Password=123123; Database=db_project; Pooling=false;Allow Zero Datetime=false;";
objConn = new MySqlConnection(strConnString);
objConn.Open();
strConnString = "INSERT INTO teachers(User_ID,Pin_ID,Prefix,T_name,T_Lastname,Sex,Brithday,Age,Salary,Position,Hous_number,Moo,District,Canton,Drorince,ZIP_code,Phone_nambers,E_mail,Road)" + " VALUES ('" + this.User_ID.Text + "','" + this.Pin_ID.Text + "','" + this.Prefix.Text + "','" + this.T_name.Text + "','" + this.T_Lastname.Text + "','" + this.Sex.Text + "','" + dt + "','" + this.Age.Text + "','" + this.Salary.Text + "','" + this.Position.Text + "','" + this.Hous_number.Text + "','" + this.Moo.Text + "','" + this.District.Text + "','" + this.Canton.Text + "','" + this.Drorince.Text + "','" + this.ZIP_code.Text + "','" + this.Phone_nambers.Text + "','" + this.E_mail.Text + "','" + this.Road.Text + "')";
//strConnString = "INSERT INTO teachers(Brithday)" + " VALUES ( '" + dt + "')";
// strConnString = "INSERT INTO teachers(Brithday)" + " VALUES ('" + dt + "')";
objCmd = new MySqlCommand(strConnString, objConn);
objCmd.ExecuteReader();
MessageBox.Show("Save Successfully.");
ได้แล้วครับ นั่ง งง ตั้งนาน สุดท้ายแค่บรรทัดเดียว
ประวัติการแก้ไข 2011-07-19 11:02:49
Date :
2011-07-19 11:01:48
By :
noi014
Load balance : Server 02