Cannot save to database or retrieve (right info) from it

夙愿已清 提交于 2019-12-22 18:21:44

问题


I am saving data to the database "successfully", using:

try
{
    // Save the new Client now.
    Profile profile = new Profile()
    {
        Salutation = Salutation,
        FirstName = FirstName,
        MiddleName = MiddleName,
        LastName = LastName,
        Gender = Gender,
        DateOfBirth = DateOfBirth,

        CompanyName = ClientCompanyName,
        StreetAddress = StreetAddress,
        Suburb = Suburb,
        PostCode = PostCode,
        State = State,
        Country = Country,

        ABN = ABN.ToString(),
        ACN = ACN.ToString(),

        TelephoneNumber = TelephoneNumber,
        MobileNumber = MobileNumber,
        EmailAddress = EmailAddress
    };
    database.Profiles.Add(profile);
    database.SaveChanges();

    Console.WriteLine("Client saved.");
}
catch (Exception exception)
{
    Console.WriteLine(exception.Message + Environment.NewLine + exception.InnerException);
}

and it says it's been saved. But when I look in the database - nothing's there!

And, when I try to get the first name of the person I just added, it returns the entire SELECT statement - not their first name:

var hisname = database.Profiles.Where(x => x.FirstName == "Jase");

What gives?

This is a C# Windows Console application.

The same code works on my website.

Update

This is what it returns to me when I try to do SELECT:


回答1:


You didn't show us your connection string yet, and from your question, it's not clear if you see this behavior only when running your app from within Visual Studio or also outside of VS.

If you happen to be using SQL Server Express and the AttachDbFileName=somename.mdf approach, and you observe this behavior when running your app inside Visual Studio, then keep on reading.

The whole User Instance and AttachDbFileName= approach is flawed - at best! 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!

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. MyDatabase)

  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=MyDatabase;Integrated Security=True
    

    and everything else is exactly the same as before...



来源:https://stackoverflow.com/questions/17199547/cannot-save-to-database-or-retrieve-right-info-from-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!