简体   繁体   English

c#本地数据库插入不起作用

[英]c# Local Database insert into doesn't work

I tried too many times but insertion doesn't work!我尝试了太多次但插入不起作用! Please help me..请帮我..

codes:代码:

SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO mytbl(Id, Nav) VALUES('yek','du')", conn); //yek and du are examples
//Following command doesn't work, too
//"INSERT INTO mytbl(Id, Nav) VALUES('"+tb.Text+"','"+tb2.Text+"')"
cmd.ExecuteNonQuery();
conn.Close();

The whole User Instance and AttachDbFileName= approach is flawed - at best!整个User Instance 和 AttachDbFileName=方法是有缺陷的 - 充其量! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\\bin\\debug - where you app runs) and most likely , your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!在 Visual Studio 中运行您的应用程序时,它将复制.mdf文件(从您的App_Data目录到输出目录 - 通常是.\\bin\\debug - 您的应用程序运行的位置),并且很可能,您的INSERT工作正常 - 但是你最终只是在看错误的 .mdf 文件

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.如果您想坚持使用这种方法,请尝试在myConnection.Close()调用上放置一个断点 - 然后使用 SQL Server Mgmt Studio Express 检查.mdf文件 - 我几乎可以肯定您的数据在那里。

The real solution in my opinion would be to我认为真正的解决方案

  1. install SQL Server Express (and you've already done that anyway)安装 SQL Server Express(无论如何你已经完成了)

  2. install SQL Server Management Studio Express安装 SQL Server Management Studio Express

  3. create your database in SSMS Express , give it a logical name (eg VictoryDatabase )SSMS Express 中创建您的数据库,给它一个逻辑名称(例如VictoryDatabase

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances.使用其逻辑数据库名称(在服务器上创建时给出)连接到它 - 不要乱用物理数据库文件和用户实例。 In that case, your connection string would be something like:在这种情况下,您的连接字符串将类似于:

     Data Source=.\\\\SQLEXPRESS;Database=VictoryDatabase;Integrated Security=True

    and everything else is exactly the same as before...其他一切都和以前完全一样......

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.另请参阅 Aaron Bertrand 的优秀博客文章要戒掉坏习惯:使用 AttachDbFileName了解更多背景信息。

Your code is working but the db(.mdf) is getting copied to \\bin\\debug directory first and the app is using that one(\\bin\\debug\\Database1.mdf).您的代码正在运行,但首先将 db(.mdf) 复制到\\bin\\debug目录,并且应用程序正在使用该目录 (\\bin\\debug\\Database1.mdf)。 Use the following code to get the db (.mdf) path which is present at your App root directory.使用以下代码获取应用程序根目录中的 db (.mdf) 路径。

string path = AppDomain.CurrentDomain.BaseDirectory.ToLower().Replace("\\bin", "").Replace("\\debug", "").Replace("\\release", "").TrimEnd('\\');

string conStr =  "Data Source=(LocalDB)\\v11.0;AttachDbFilename=" + path + "\\Database1.mdf;Integrated Security=True";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM