是否有插入的方式if/else語句到下面一行在這裏:有條件的SQL語句 - 插入/更新查詢之間切換
command.CommandText = "UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3";
在一個情況下ID =值3(換句話說,行已存在)可以發現,然後更新查詢需要的地方,但它無法找到我想要插入查詢發生:
command.CommandText = "INSERT INTO Table1 (ID,Team) VALUES (value1,value2)";
採取地方...我怎麼能做到這一點?
private void button1_Click(object sender, EventArgs e)
{
// save to access database when user clicks on the save button
using (OleDbConnection conn = new OleDbConnection())
{
//the file path of mdb
string filepath = @"C:\Users\sy\Visual Studio 2008\Projects\demo\demo\CE_Database.mdb";
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" + filepath + ";";
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
//your update satemenet
command.CommandText = "UPDATE Table1 SET ID=value1,Team=value2 WHERE ID=value3";
conn.Open();
//update to ms access
command.ExecuteNonQuery();
conn.Close();
}
}
你怎麼知道該行是否存在?您是否查詢數據庫,以便您在某個時間點存在Id? – scheien
注意您不必在'using'塊中明確關閉連接。 –
@ZevSpitz:真的嗎?參考? – dotNET