C# - 的ExecuteNonQuery()不与SQL Server CE工作 [英] C# - ExecuteNonQuery() isn't working with SQL Server CE

查看:140
本文介绍了C# - 的ExecuteNonQuery()不与SQL Server CE工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过应该被添加到数据库文件(将.sdf)用户inputed一些数据。我选择Sql Server的CE,因为这个应用程序是非常小的,我并没有看到需要一个基于服务的数据库工作。

I got some data inputed by the user that should be added to a Database File (.sdf). I've choose Sql Server CE because this application is quite small, and i didn't saw need to work with a service based database.

好吧任何方式。

下面那张代码:

public class SqlActions
{
    string conStr = String.Format("Data Source = " + new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\basedados.sdf");

    public SqlCeConnection SQLCEConnect()
    {
        SqlCeConnection Connection = new SqlCeConnection(conStr);
        Connection.Open();
        return Connection;
    }

    public Boolean AdicionarAuditorio(string Nome, int Capacidade)
    {
        string Query = "INSERT INTO auditorios (nome, capacidade) VALUES (@Nome, @Capacidade)";
        using (var SQLCmd = new SqlCeCommand(Query, SQLCEConnect()))
        {
            SQLCmd.Parameters.AddWithValue("@Nome", Nome);
            SQLCmd.Parameters.AddWithValue("@Capacidade", Capacidade);
            if (SQLCmd.ExecuteNonQuery() == 1)
            {
                return true;
            } else {
                return false;
            }

        }
    }
}

我用 AdicionarAuditorio(字符串诺姆,诠释Capacidade)函数来插入数据。运行的ExecuteNonQuery()这是应该后,他返回受影响的行数为运行查询。
所以,它应该返回 1 如果查询是成功的,对吧?

I use the AdicionarAuditorio(string Nome, int Capacidade) function to Insert the data. running ExecuteNonQuery() which is supposed to return the number of affected rows after he as run the query. So it should return 1 if the query as successful, right?

在最后,他将返回1,但如果我的浏览器表数据,数据查询应补充的是不存在的。

In the end he returns 1, but if I browser the table data, the data that the query should add isn't there.

所以,什么是错在这里?

So whats wrong here?

请注意。如果你的想法是,
的问题是连接:我看不到
为什么这个问题一旦我使用该
连接功能的一些
Select语句 SQLCEConnect()
和他们都工作得很好。

NOTE. If your thinking that the problem is the connection: I can't see why is the problem once i got some Select statements that use that connection function SQLCEConnect() and they all work pretty well.

感谢。

推荐答案

你确定你正在寻找正确的文件?当你建立你的VS应用程序,它会将自卫队文件内容到目标文件夹,所以在您的项目数据库将不反映任何更新。你的代码是有拿起文件位置。

Are you sure you are looking at the right file? When you build your app in VS, it copies the SDF file as content to the target folder, so the database in your project will not reflect any updates. Your code is picking up the the file location there.

这是BTW不是一个好的做法,因为一旦部署,程序文件夹是不可写你的应用程序(会这样是问题 - 你已经部署?)。相反,数据库文件应驻留在你的应用程序数据文件夹中。

This is btw not a good practice, because once deployed, the program folders are not writable to your app (could this be the problem - did you already deploy?). Instead, the database file should reside in your appdata folder.

这篇关于C# - 的ExecuteNonQuery()不与SQL Server CE工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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