SQL CE 3.5 - 写入数据库 [英] SQL CE 3.5 - Writing to a database

查看:38
本文介绍了SQL CE 3.5 - 写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写入数据库,我一直在使用 关注网站.特别是 LoadARow 函数.

I'm trying to write to a data base, I have been working of the following site. In particular the LoadARow function.

我一直收到运行时错误

System.IndexOutOfRangeException:此 SqlCeParamaterCollection 不包含 ParameterName 为Value"的 SqlCeParameter.

"System.IndexOutOfRangeException: A SqlCeParameter with ParameterName 'Value' is not contained by this SqlCeParamaterCollection.

错误引用了command.Parameters["@Value"].Value = num;"行.我使用的数据库有一个设置为键的值列和一个文本列.

The error references the line "command.Parameters["@Value"].Value = num;". The database I'm using has a Value column set as the key, and a text column.

using (SqlCeConnection connect = new SqlCeConnection("Data Source=C:\\Users\\mike\\Documents\\database.sdf"))
{
  connect.Open();

  string text = "test";
  int num = 0;

  using (SqlCeCommand command = new SqlCeCommand("insert into Data Table values (@Value, @Text)", connect))
  {
    command.Parameters.Add("@Text", SqlDbType.NVarChar);
    command.Parameters["@Value"].Value = num;
    command.Parameters.AddWithValue("@Text", text);
    command.ExecuteNonQuery();
  }
}

推荐答案

尝试:

using (SqlCeConnection connect = new SqlCeConnection("Data Source=C:\\Users\\mike\\Documents\\database.sdf"))
    {
        connect.Open();

        string text = "test";
        int num = 0;

        using (SqlCeCommand command = new SqlCeCommand("insert into Data Table values (@Value, @Text)", connect))
        {
           command.Parameters.Add("@Value", SqlDbType.NVarChar, num);
           command.Parameters.Add("@Text", SqlDbType.NVarChar, text);
           command.ExecuteNonQuery();
        }
    }

这篇关于SQL CE 3.5 - 写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆