寫入Access檔案

寫入Access檔案

用到了筆記下來一下

 

  string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath.Text + ";Persist Security Info=False";
           
            OleDbConnection conn = new OleDbConnection(strConnection);
            #region  一般測試寫法
            //string insert = "insert into AdPlayList (FileName,FileDate,FileSize,OrderID,Sha1) Values ('23','2012/10/10','1','1','1')";
            //OleDbCommand cmd = new OleDbCommand(insert,conn);
            //conn.Open();
            //cmd.ExecuteNonQuery();
            //conn.Close();
            #endregion

            OleDbDataAdapter da = new OleDbDataAdapter();
            da.InsertCommand = new OleDbCommand("insert into AdPlayList (FileName,FileDate,FileSize,OrderID,Sha1)values(@FileName,@FileDate,@FileSize,@OrderID,@Sha1)", conn);
            da.InsertCommand.Parameters.Add("@FileName", OleDbType.VarChar, 40).Value=textBox2.Text;
            da.InsertCommand.Parameters.Add("@FileDate", OleDbType.DBDate, 20).Value= DateTime.Now.ToShortDateString();
            da.InsertCommand.Parameters.Add("@FileSize", OleDbType.Integer).Value= int.Parse(textBox3.Text);
            da.InsertCommand.Parameters.Add("@OrderID", OleDbType.Integer).Value= int.Parse(textBox4.Text);
            da.InsertCommand.Parameters.Add("@Sha1", OleDbType.VarChar, 40).Value = textBox5.Text;
            conn.Open();
            da.InsertCommand.ExecuteNonQuery();
            conn.Close();
          
            da = new OleDbDataAdapter("select * from AdPlayList", conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "AdPlayList");
            dataGridView1.DataSource = ds.Tables[0];
            da.Dispose();

 

 

 

 

 

大家一起加入blogads 賺零用錢!!