ADO.Net - 为什么插入没有发生?
我浏览了 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 位。
更新
以前我使用项目中存在的 MDF 文件。我猜想,每次项目运行时它都会自动附加到 SQL Server 实例。这是我遇到问题的时候。连接字符串包含一些有关附加数据库文件的信息。
我删除了与 Visual C# 2010 Express 一起安装的 SQL Server 2008 Express。还从项目中删除了 MDF 文件。
我单独下载并安装了 SQL Server 2008 Express 和 Management Studio Express。
在 Management Studio 中创建了一个新数据库。
使用不同类型的连接字符串来使用服务器中的数据库。
现在插入工作了!
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
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.
I removed the SQL Server 2008 Express that I installed along with Visual C# 2010 Express. Also removed the MDF file from the project.
I Separately downloaded and installed SQL Server 2008 Express along with Management Studio Express.
Created a new database in management studio.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您遇到了以下情况:
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 itServer=.\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 toApplicationProjectFolder\bin\Debug
, so that when the application was run, the filedatabase.mdf
was present. This means that everytime you ran your project, the "empty"database.mdf
file was copied fromApplicationProjectFolder\database.mdf
toApplicationProjectFolder\bin\Debug\database.mdf
, hence the data "disappearing". Also, the filedatabase.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.