0
這是mycode的 請幫我如何在C#中使用INSERT INTO訪問數據庫
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|members list.mdb");
OleDbCommand cmdoledb = new OleDbCommand();
con.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO Members(name,family,age,Juncture scholarships) values('" + t1.Text + "','" + t2.Text + "','" + t3.Text + "','" + t4.Text + "')",con);
cmd.ExecuteNonQuery();
show();
t1.Text = string.Empty;
t2.Text = string.Empty;
t3.Text = string.Empty;
t4.Text = string.Empty;
}
private void show()
{
string d;
OleDbConnection con;
OleDbDataAdapter adap;
DataSet ds = new DataSet();
con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|members list.mdb");
d = "select * from members";
adap = new OleDbDataAdapter(d, con);
adap.Fill(ds, "members");
dataGridView1.DataSource = ds.Tables["members"];
}
當我使用這個代碼,我得到這個錯誤
類型的未處理的異常「系統。 Data.OleDb.OleDbException' 發生在System.Data.dll中
其他信息:INSERT INTO語句中的語法錯誤。
注意:您查詢SQL注入敞開的,所以你甚至不真控制你正在執行的語法。查詢參數在這裏非常有用。 – David