简体   繁体   English

在ASP.NET C中将数据插入Visual Studio LocalDB#

[英]Insert data into Visual Studio LocalDB in ASP.NET C#

I have a Windows web application and have created a .mdf file in the App_Data folder and I am trying to insert data into the tables already created. 我有一个Windows Web应用程序,并在App_Data文件夹中创建了一个.mdf文件,我正在尝试将数据插入到已创建的表中。

It is able to retrieve codes with a basic SELECT * statement, but I can't insert data, this is the code I have: 它能够使用基本的SELECT *语句检索代码,但我无法插入数据,这是我的代码:

private void Update(string username)
{
    SqlConnection con = new SqlConnection(@"MYSQLCONNECTION");
    con.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    string bcd = "INSERT INTO NameList(name) VALUES (@paraUsername)";
    cmd = new SqlCommand(bcd, con);
    cmd.Parameters.AddWithValue("@paraUsername", username);

    cmd.ExecuteNonQuery();
    list.Update();

    con.Close();
}

This code has worked for me in the past, but this time round it does not work. 这段代码过去对我有用,但这一次它不起作用。 There is no error shown, but the data is not inserted into the table. 没有显示错误,但数据未插入表中。

This is how I call the method in side my button_click method: 这就是我在button_click方法中调用方法的方法:

Update(tbName.text);

What might be the problem? 可能是什么问题?

Assuming you have a 假设你有一个

AttachDbFileName=......

section in your connection string - then this is a known issue: 您的连接字符串中的部分 - 然后这是一个已知问题:

The whole AttachDbFileName= approach is flawed - at best! 整个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 YourDatabase ) SSMS Express中创建数据库,给它一个逻辑名称(例如YourDatabase

  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=YourDatabase;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的优秀博客文章Bad bad to kick:使用AttachDbFileName获取更多背景信息。

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

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