【问题标题】:insert data into database in windows mobile 6在windows mobile 6中将数据插入数据库
【发布时间】:2012-11-16 06:27:08
【问题描述】:

我是 windows mobile 6 的初学者,我试图将值传递给数据库,但 itz 不工作没有错误和警告请检查我的代码并帮助我.. 数据库没有更新..

private void button1_Click(object sender, EventArgs e)
{
  string conSTR = "data source= " +
    (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) +
    "\\DPass.sdf; Persist security info=False";


  SqlCeConnection connection = new SqlCeConnection(conSTR);
  SqlCeCommand cmd = new SqlCeCommand("INSERT INTO cusTable(Fname,Lname) VALUES(@Fname,@Lname)",connection);
  cmd.Parameters.AddWithValue("@Fname", textBox1.Text);
  cmd.Parameters.AddWithValue("@Lname", textBox2.Text);

  connection.Open();
  int affectedRows = cmd.ExecuteNonQuery();
  cmd.ExecuteNonQuery();
  MessageBox.Show("ela");
  connection.Close();
}

【问题讨论】:

    标签: windows-mobile compact-framework sql-server-ce


    【解决方案1】:

    虽然不是解决方案的一部分,但最好将其移到循环之外,这样您就不必一遍又一遍地阅读。

    private readonly string CON_STR = "data source= " +
        (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) +
        "\\DPass.sdf; Persist security info=False";
    

    现在,使用 Try/Catch 语句找出您做错了什么。

    在下面修改后的代码中,我包含了其中的 2 个。外部的 Try/Catch 设置为 Exception 并且会吞下任何异常。内部的 Try/Catch 是我怀疑会抛出消息的那个,它是一个 SqlCeException,它只针对 SqlCe 错误抛出。

    private void button1_Click(object sender, EventArgs e)
    {
       int affectedRows = 0;
       string sqlText = "INSERT INTO cusTable(Fname,Lname) VALUES(@Fname,@Lname)";
       try {
         using (var cmd = new SqlCeCommand(sqlText, new SqlCeConnection(CON_STR))) {
           cmd.Parameters.AddWithValue("@Fname", textBox1.Text);
           cmd.Parameters.AddWithValue("@Lname", textBox2.Text);
           try {
             cmd.Connection.Open();
             affectedRows = cmd.ExecuteNonQuery();
             //cmd.ExecuteNonQuery(); <= ?? Why are you calling this twice?
           } catch (SqlCeException ceEr) {
             MessageBox.Show(ceEr.Message, "SqlCe Error");
           } finally {
             cmd.Connection.Close();
           }
         }
       } catch (Exception err) {
         MessageBox.Show(err.Message, "Non-SqlCe Error");
       }
       MessageBox.Show(string.Format("Affected Rows: {0}", affectedRows), "ela");
    }
    

    您似乎还调用了两次INSERT 函数,因此第二次ExecuteNonQuery() 调用已被注释掉。

    运行此代码,并报告错误消息是什么以及该 MessageBox 的标题是“SqlCe 错误”还是“非 SqlCe 错误”。

    【讨论】:

    • @jp2codem 我用你的方式试过了。没有错误但仍然无法执行所有代码最终消息框也来了但数据库表没有更新..请帮助我非常感谢您
    • 我做了一个小修改,将string.Format("Affected Rows: {0}", affectedRows) 代码添加到消息框。如果这返回值 1,则您的数据库正在更新。如果您没有看到更新,则可能是检查了错误的数据库。 (它发生了)
    • yeez 你是对的......数据库正在模拟器内更新......非常感谢你的帮助...... :)
    【解决方案2】:

    虽然我还没有使用您的方式在 SQL CE 数据库中插入数据,但您的查询字符串似乎缺少一些项目。看着http://msdn.microsoft.com/en-us/library/aa977880%28v=vs.71%29.aspx,我看到你正在使用这种语法:

    INSERT INTO employee (emp_no, fname, lname, officeno) ;VALUES (3022, "John", "Smith", 2101)
    

    但在您的查询中,字段列表后缺少分号。

    其次,如果您需要在数据库中插入字符串信息,这些必须用引号括起来。

    所以请尝试以下查询字符串:

    "INSERT INTO cusTable(Fname,Lname); VALUES(\"@Fname\",\"@Lname\")"
    

    顺便说一句:如果代码中的 SQL 语句不起作用,我会使用 MS Access 或 SQL Manager 来测试我的查询字符串。

    问候

    约瑟夫

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      相关资源
      最近更新 更多