关于使用C#向access数据库表update数据时的一个问题!
此为我代码:privatestaticvoidUpdateBase(doublekafang,stringword,stringbasePath){stringconns...
此为我代码:
private static void UpdateBase(double kafang, string word, string basePath)
{
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + basePath;
using (OleDbConnection conn = new OleDbConnection(connstr))
{
try
{
conn.Open();
string sql = @"UPDATE WORDCOUNT1 SET [KAFANG] = " + kafang + " WHERE WORD = " + word;
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
}
}
数据库字段格式:
调试一直显示,至少一个参数没有被指定值,请问错在哪里? 展开
private static void UpdateBase(double kafang, string word, string basePath)
{
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + basePath;
using (OleDbConnection conn = new OleDbConnection(connstr))
{
try
{
conn.Open();
string sql = @"UPDATE WORDCOUNT1 SET [KAFANG] = " + kafang + " WHERE WORD = " + word;
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine("异常:" + ex.Message);
}
}
}
数据库字段格式:
调试一直显示,至少一个参数没有被指定值,请问错在哪里? 展开
2个回答
展开全部
sql语句要求字符串类型字段的value两边有单引号,改成
string sql = @"UPDATE WORDCOUNT1 SET [KAFANG] = '" + kafang + "' WHERE WORD = '" + word + "'";
//或者
//string sql = string.Format(@"UPDATE WORDCOUNT1 SET [KAFANG] = '{0}' WHERE WORD = '{1}'", kafang, word);
还有更好的参数化查询,也就是用OleDbParameter之类的,可以查一下。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询