SQL insert query in C# does not update database

后端 未结 2 473
深忆病人
深忆病人 2021-01-28 06:51

This is my code below. The program runs fine. I don\'t get any exceptions. But in the end, the table doesn\'t contain the newly inserted row. (Note, the connection string is co

2条回答
  •  天命终不由人
    2021-01-28 07:10

    First of all: start using parametrized queries! Otherwise your code is always open to SQL injection attacks - not something you want to have to deal with.

    Assuming you are using this feature (depends on the connection string, which you haven't shown us in the original question): the whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

    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.

    The real solution in my opinion would be to

    1. install SQL Server Express (and you've already done that anyway)

    2. install SQL Server Management Studio Express

    3. create your database in SSMS Express, give it a logical name (e.g. 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...

提交回复
热议问题