0
有什麼錯我的代碼,我只是想將數據添加到訪問數據庫,但它表明ExecuteNonQuery
:的ExecuteNonQuery:Connection屬性尚未初始化(Access數據庫)
連接屬性尚未初始化。
這很奇怪,因爲在其他項目代碼類似於此工程就好了。
OleDbCommand command = new OleDbCommand();
OleDbConnection connect = new OleDbConnection();
OleDbDataReader reader;
public Absen()
{
InitializeComponent();
}
MainForm form_utama;
private void Absen_Load(object sender, EventArgs e)
{
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Visual Studio Project\Minor baru - back up\Minor baru\Absensi.accdb;Persist Security Info=False;";
}
private void button1_Click(object sender, EventArgs e)
{
if (idkaryawantxt.Text != "")
{
string q = "insert into tableAbsensi (ID,ID_divisi,Waktu,Tanggal) values ('" + idkaryawantxt.Text.ToString() + "','" + iddivisitxt.Text.ToString() + "','" + (DateTime.Now.ToString("hh:mm :")) + "','" + (DateTime.Now.ToString("MM-dd-yyyy")) + "')";
dosomething(q);
}
}
private void dosomething(String q)
{
try
{
connect.Open();
command.CommandText = q;
command.ExecuteNonQuery();
connect.Close();
}
catch (Exception e)
{
connect.Close();
MessageBox.Show(e.Message.ToString());
}
}
+1另外不要忘了** **處置所有這些對象(未使用時的連接和執行後的命令)。 –
謝謝!你幫我〜 雖然XD很有趣 – wendy