简体   繁体   English

C#无法将数据插入SQL Server数据库

[英]C# cannot insert data into SQL Server database

I'm recently new at C# and follow a tutorial on connection to the database, the connection for viewing or SELECT command was ok, I made some revision based on the threads of forums to make my code better, but when I was in INSERT for my SQL Server, I have some problems. 我最近刚接触C#,并按照有关数据库连接的教程进行操作,用于查看或SELECT命令的连接是可以的,我根据论坛的线程进行了一些修订,以使我的代码更好,但是当我在INSERT上时我的SQL Server,我有一些问题。 I was able to insert the data but when I re-run the program the data that I just inserted earlier was not in the database. 我能够插入数据,但是当我重新运行程序时,我刚才插入的数据不在数据库中。

Here is my code on insert 这是我插入的代码

string commandText = "INSERT INTO loginInfo (name, pass, role) VALUES(@name, @pass, @role)";

using (connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(commandText, connection);
    command.CommandType = CommandType.Text;

    command.Parameters.AddWithValue("@name", textBox1.Text);
    command.Parameters.AddWithValue("@pass", textBox2.Text);
    command.Parameters.AddWithValue("@role", textBox3.Text);

    try
    {
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
    catch
    {
    }
    finally
    {
        command.Connection.Close();
    }
}

I'm trying to create a simple CRUD but not save the data... please help 我正在尝试创建一个简单的CRUD,但未保存数据...请帮助

If you're using the AttachDbFileName= clause in your connection string, you might have fallen victim to a well-known issue : you might just be looking at the wrong database (file) when checking your data. 如果您在连接字符串中使用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 connection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there. 如果您想使用这种方法,请尝试在connection.Close()调用上放置一个断点-然后使用SQL Server Mgmt Studio Express检查.mdf文件-我几乎可以确定您的数据在那里。

The long-term viable 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)出色的博客文章不良习惯:使用AttachDbFileName获取更多背景信息。

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

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