如何在添加
后删除项目本地数据库 add("01:23:45:67:89:ac", "yes"); //add new item
static void add(string id, string input)
{
using (SqlCeConnection connection = new SqlCeConnection("datasource=base.sdf"))
{
connection.Open();
try
{
using (SqlCeCommand command = new SqlCeCommand("INSERT INTO userinput VALUES(@macadres, @userinput)", connection))
{
command.Parameters.Add(new SqlCeParameter("macadres", id));
command.Parameters.Add(new SqlCeParameter("userinput", input));
command.ExecuteNonQuery();
}
}
catch
{
Console.WriteLine("Not OK.");
}
connection.Close();
}
}
需要删除添加01:23:45:67:89:ac
DeleteRecord( “01:23:45:67:89:AC”);
static void DeleteRecord(string id)
{
using (SqlCeConnection connection = new SqlCeConnection("datasource=base.sdf"))
{
connection.Open();
try
{
using (SqlCeCommand command = new SqlCeCommand("DELETE FROM userinput WHERE macadres ='" + id + "'"))
{
command.Connection.Open();
command.ExecuteNonQuery();
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
connection.Close();
}
}
答案 0 :(得分:1)
DeleteRecord("01:23:45:67:89:ac");
static void DeleteRecord(string id)
{
using (SqlCeConnection connection = new SqlCeConnection("datasource=base.sdf"))
{
connection.Open();
try
{
using (SqlCeCommand command = new SqlCeCommand("DELTE FROM userinput WHERE macadres ='"+ id +"'", connection);
{
command.ExecuteNonQuery();
}
}
catch
{
Console.WriteLine("Not OK.");
}
finally
{
connection.Close();
}
}
}
或者您可以通过提供连接和查询来创建一个删除任何记录的方法
string connectionstring = "datasource=base.sdf"; // "Data Source = MyDatabase.sdf; Password ='<pwd>'"
string deleteQuery = "DELTE FROM userinput WHERE macadres ='"+ id +"'";
你可以打电话给下面的
MyExecuteNonQuery( deleteQuery , connectionstring);
这里即方法实施
private void MyExecuteNonQuery(string queryString,
string connectionString)
{
SqlCeConnection conn = null;
try
{
conn = new SqlCeConnection(connectionString);
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = queryString;
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}