我在这里搜索了我的问题,但找不到。我正在使用 Microsoft VS 2010 C#。
这是我的代码:
private OleDbConnection myCon;
public Form5()
{
myCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|ForeignWorkerinfo.accdb");
InitializeComponent();
}
private void Form5_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'foreignWorkerinfoDataSet.FWinFO' table. You can move, or remove it, as needed.
this.fWinFOTableAdapter.Fill(this.foreignWorkerinfoDataSet.FWinFO);
}
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = myCon;
cmd = new OleDbCommand("INSERT INTO [FWinFO] ([ID], [Name], [Gender], [Date of Birth], [Country], [Date of Expire], [Passport No], [Working Place]) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
cmd.Parameters.AddWithValue("@id", textBox8.Text);
cmd.Parameters.AddWithValue("@name", textBox1.Text);
cmd.Parameters.AddWithValue("@gender", textBox2.Text);
cmd.Parameters.AddWithValue("@dob", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@country", textBox4.Text);
cmd.Parameters.AddWithValue("@doe", dateTimePicker2.Value);
cmd.Parameters.AddWithValue("@passport", textBox6.Text);
cmd.Parameters.AddWithValue("@workplace", textBox7.Text);
cmd.ExecuteNonQuery();
myCon.Close();
}
谁能告诉我为什么连接属性没有被初始化?