ADO.Net - 为什么插入没有发生?

发布于 2024-09-19 15:14:50 字数 1332 浏览 6 评论 0原文

我浏览了 MSDN 页面来学习使用命令的 ADO.Net。我可以使用那里发布的示例代码进行阅读。

但是当我尝试使用下面的修改代码时,插入没有发生。我不知道为什么。有人可以告诉我这段代码有什么问题吗?

string connectionString = "A_VALID_CONNECTION_STRING";
string commandText = 
"INSERT INTO Contacts (FullName, Mobile) VALUES ('Pierce Brosnan', '1800-007')";

SqlConnection connection = new SqlConnection(connectionString);          

try
{
  connection.Open();
  SqlCommand command = new SqlCommand(commandText, connection);
  Console.WriteLine(command.ExecuteNonQuery());
  connection.Close();
}
catch (Exception ex)
{
  Console.WriteLine(ex.Message);
}

编辑

  • 不引发异常。

  • 应该返回 no 的 ExecuteNonQuery()。受影响的行返回 1

  • 环境:Visual C# 2010 Express | SQL Server 2008 Express | SQL Server 2008 Express Windows 7 Ultimate 32 位。

更新

  1. 以前我使用项目中存在的 MDF 文件。我猜想,每次项目运行时它都会自动附加到 SQL Server 实例。这是我遇到问题的时候。连接字符串包含一些有关附加数据库文件的信息。

  2. 我删除了与 Visual C# 2010 Express 一起安装的 SQL Server 2008 Express。还从项目中删除了 MDF 文件。

  3. 我单独下载并安装了 SQL Server 2008 Express 和 Management Studio Express。

  4. 在 Management Studio 中创建了一个新数据库。

  5. 使用不同类型的连接字符串来使用服务器中的数据库。

现在插入工作了!

PS 我想我应该提到我有一个附加数据库文件场景。真的很抱歉。

I went through MSDN pages to learn ADO.Net using Commands. I am able to read using the sample code posted there.

But when I tried to use the modification code below, the insert is not happening. I am not ale to figure out why. Can someone please tell me what is wrong with this code?

string connectionString = "A_VALID_CONNECTION_STRING";
string commandText = 
"INSERT INTO Contacts (FullName, Mobile) VALUES ('Pierce Brosnan', '1800-007')";

SqlConnection connection = new SqlConnection(connectionString);          

try
{
  connection.Open();
  SqlCommand command = new SqlCommand(commandText, connection);
  Console.WriteLine(command.ExecuteNonQuery());
  connection.Close();
}
catch (Exception ex)
{
  Console.WriteLine(ex.Message);
}

Edit

  • No exception is thrown.

  • The ExecuteNonQuery() which is supposed to return the no. of rows affected is returning 1.

  • Environment: Visual C# 2010 Express | SQL Server 2008 Express | Windows 7 Ultimate 32-bit.

Update

  1. Previously I was using a MDF file present in the project. It was, I guess, automatically attached to the SQL server instance each time the project ran. This is when I had the problem. The connection string had some info about attaching a database file.

  2. I removed the SQL Server 2008 Express that I installed along with Visual C# 2010 Express. Also removed the MDF file from the project.

  3. I Separately downloaded and installed SQL Server 2008 Express along with Management Studio Express.

  4. Created a new database in management studio.

  5. Used a different type of connection string to use the database in the server.

Now INSERT is working!

P.S. I guess I should have mentioned that I had an attach database file scenario. Really sorry for that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2024-09-26 15:14:50

我怀疑您遇到了以下情况:

  • 项目中存在 Database.mdf 文件,并在其中创建了表结构
  • 您的连接字符串看起来像这样 Server=.\SQLExpress;AttachDbFilename =database.mdf;数据库=dbname; Trusted_Connection=Yes;,即在连接字符串中加载数据库。

发生的情况是,当您构建/运行项目时,您的应用程序已编译,database.mdf 文件也随之复制到 ApplicationProjectFolder\bin\Debug,因此当应用程序运行时,文件 database.mdf 就存在。这意味着每次运行项目时,“空”database.mdf 文件都会从 ApplicationProjectFolder\database.mdf 复制到 ApplicationProjectFolder\bin\Debug\database .mdf,因此数据“消失”。此外,文件 database.mdf 可能在项目中的属性上设置了“始终复制”。

所以,“INSERT”正在工作,它只是在每次运行应用程序时“重置”。

My suspicion is that you had the following scenario:

  • Database.mdf file was present in the project with the table structure created in it
  • Your connection string looked something like this Server=.\SQLExpress;AttachDbFilename=database.mdf;Database=dbname; Trusted_Connection=Yes;, i.e. loading the database in the connection string.

What was happening was, when you built/ran your project, your application was compiled and the database.mdf file was copied along with it to ApplicationProjectFolder\bin\Debug, so that when the application was run, the file database.mdf was present. This means that everytime you ran your project, the "empty" database.mdf file was copied from ApplicationProjectFolder\database.mdf to ApplicationProjectFolder\bin\Debug\database.mdf, hence the data "disappearing". Also, the file database.mdf probably had "Copy Always" set on its properties in the project.

So, the "INSERT" was working, it was just being "reset" everytime you ran your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文