How to delete item local database
How to remove items local database after added
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))
{
开发者_Go百科 command.Parameters.Add(new SqlCeParameter("macadres", id));
command.Parameters.Add(new SqlCeParameter("userinput", input));
command.ExecuteNonQuery();
}
}
catch
{
Console.WriteLine("Not OK.");
}
connection.Close();
}
}
Need to remove added 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();
}
}
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();
}
}
}
Or you can create one method for delete any record by giving connection and query as bellow
string connectionstring = "datasource=base.sdf"; // "Data Source = MyDatabase.sdf; Password ='<pwd>'"
string deleteQuery = "DELTE FROM userinput WHERE macadres ='"+ id +"'";
you can call as bellow
MyExecuteNonQuery( deleteQuery , connectionstring);
here ie the method implimentation
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();
}
}
精彩评论