无法保存到数据库或从中检索(右侧信息) [英] Cannot save to database or retrieve (right info) from it

查看:154
本文介绍了无法保存到数据库或从中检索(右侧信息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法将数据成功保存到数据库:

  try 
{
//现在保存新客户端。
个人资料profile = new Profile()
{
Salutation = Salutation,
FirstName = FirstName,
MiddleName = MiddleName,
LastName = LastName,
性别=性别,
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(异常异常)
{
Console.WriteLine(exception.Message + Environment.NewLine + exception.InnerException);
}

,表示已保存。但是当我查看数据库 - 没有什么!



而且,当我试图得到刚刚添加的人的名字,它返回整个SELECT语句 - 不是他们的名字



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



有什么作用?



这是一个C#Windows控制台应用程序。



相同的代码可在我的网站上使用。



更新



这是我尝试做SELECT时返回给我的信息:



解决方案

你没有向我们显示你的连接字符串,从你的问题,它不清楚如果你看到这种行为只有当从Visual Studio或VS之外运行你的应用程序。



如果你碰巧使用SQL Server Express AttachDbFileName = somename.mdf 方法,



整个用户实例和 AttachDbFileName = 方法有缺陷 - 最多!在Visual Studio中运行应用程序时,它会将 .mdf 文件(从您的 App_Data 目录复制到输出目录 - 通常 .\bin\debug - 应用程式执行的位置)和最可能 INSERT 工作正常 - 但你只是看着错误的.mdf文件到底!



如果您想坚持这种方法,请尝试在 myConnection.Close()调用 - 然后用SQL Server Mgmt Studio Express检查 .mdf 文件 - 我几乎肯定你的数据在那里。



真正的解决方案在我看来是


  1. 安装SQL Server Express


  2. 安装SQL Server Management Studio Express

    SSMS Express 中创建数据库,给它一个逻辑名称(例如 MyDatabase


  3. 使用其逻辑数据库名称(在服务器上创建它时)连接到它 - 不要混淆物理数据库文件和用户实例。在这种情况下,您的连接字符串将是:

     数据源= .\\SQLEXPRESS; Database = MyDatabase;集成安全= True 

    ,其他一切都是完全 ..



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:

解决方案

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

这篇关于无法保存到数据库或从中检索(右侧信息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆