我得到了一些用户输入的数据,应该将其添加到数据库文件(.sdf)中。我选择了Sql Server CE,因为这个应用程序非常小,我没有看到需要使用基于服务的数据库。C# - ExecuteNonQuery()与SQL Server CE不兼容
那么任何方式。
这里去代码:
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(string Nome, int Capacidade)
功能的嵌入式数据。运行ExecuteNonQuery()
,应该在运行查询后返回受影响的行数。 所以它应该返回1
如果查询成功了吧?
最后他返回1,但如果我浏览表数据,查询应该添加的数据不存在。
那么这里有什么错?
注意。如果你认为 问题是连接:我看不到 为什么一旦我有一些问题 选择语句,使用 连接函数
SQLCEConnect()
,他们都工作得很好。
在此先感谢。
只是为了澄清,当您将'SQLCmd.ExecuteNonQuery'的结果存储到临时变量时,该值为'1' - 正确吗? – 2010-02-03 15:36:19
是的。根据Visual Studio Debugger,临时变量值为1。 – 2010-02-03 15:41:33