关于使用C#对access数据库表中记录按某字段升序删除的问题!
下面是表的结构:这是我的代码:classProgram{staticvoidMain(string[]args){stringbasePath=@"D:\Doc\word...
下面是表的结构:
这是我的代码:
class Program
{
static void Main(string[] args)
{
string basePath = @"D:\Doc\wordcount.mdb";
string strDel = "delete top 300 * from [wordcount1] order by [Word] asc";
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + basePath;
using (OleDbConnection conn = new OleDbConnection(strConn))
{
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = strDel;
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
}
}
}
运行提示delete语句语法错误。。我对数据库操作不是很熟悉,我想做的是按“Word”这一字段升序排序删除前300条记录,请问该如何修改我的程序,请教各位大神! 展开
这是我的代码:
class Program
{
static void Main(string[] args)
{
string basePath = @"D:\Doc\wordcount.mdb";
string strDel = "delete top 300 * from [wordcount1] order by [Word] asc";
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + basePath;
using (OleDbConnection conn = new OleDbConnection(strConn))
{
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = strDel;
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
}
}
}
运行提示delete语句语法错误。。我对数据库操作不是很熟悉,我想做的是按“Word”这一字段升序排序删除前300条记录,请问该如何修改我的程序,请教各位大神! 展开
1个回答
展开全部
access是轻量级数据库,恐怕一条语句无法实现.
方法一:
如果你就做一次,那么直接去access中执行语句
select top 300 * from [wordcount1] order by [Word] asc
在查询的结果表中直接全选删除.这种方法最简单,但不适合重用.
方法二:
需要分步处理了.这个是查询方法.
public DataTable Select(string sql)//通用查询方法
{
DataSet ds = new DataSet();
System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(strConn);
System.Data.OleDb.OleDbDataAdapter _adapter;
try
{
if (connection.State != System.Data.ConnectionState.Open)
connection.Open();
_adapter = new System.Data.OleDb.OleDbDataAdapter(sql, connection);
_adapter.Fill(ds);
}
finally
{
connection.Dispose();
connection.Close();
}
return ds.Tables[0];
}
调用:
DataTable dt = Select("
select top 300 * from [wordcount1] order by [Word] asc
");
然后循环删除 for (int i=0;i<dt.Rows.Count;i++)
string del = "delete from [wordcount1] where id=" + dt.Rows[i]["Id"].ToString();
方法一:
如果你就做一次,那么直接去access中执行语句
select top 300 * from [wordcount1] order by [Word] asc
在查询的结果表中直接全选删除.这种方法最简单,但不适合重用.
方法二:
需要分步处理了.这个是查询方法.
public DataTable Select(string sql)//通用查询方法
{
DataSet ds = new DataSet();
System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(strConn);
System.Data.OleDb.OleDbDataAdapter _adapter;
try
{
if (connection.State != System.Data.ConnectionState.Open)
connection.Open();
_adapter = new System.Data.OleDb.OleDbDataAdapter(sql, connection);
_adapter.Fill(ds);
}
finally
{
connection.Dispose();
connection.Close();
}
return ds.Tables[0];
}
调用:
DataTable dt = Select("
select top 300 * from [wordcount1] order by [Word] asc
");
然后循环删除 for (int i=0;i<dt.Rows.Count;i++)
string del = "delete from [wordcount1] where id=" + dt.Rows[i]["Id"].ToString();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询