【发布时间】:2010-02-03 15:25:04
【问题描述】:
我收到了一些用户输入的数据,这些数据应该添加到数据库文件 (.sdf) 中。我选择了 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——对吗? -
是的。根据 Visual Studio Debugger,临时 var 的值为 1。
标签: c# sql sql-server-ce