不能保存到数据库或检索(右资讯)从中 [英] Cannot save to database or retrieve (right info) from it

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

问题描述

我将数据保存到数据库中成功,使用:

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!

和,当我尝试得到我刚才添加的人的名字,它返回整个SELECT语句 - 的不是他们的名字的:

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 = GT; x.FirstName ==Jase);

是什么让

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

This is a C# Windows Console application.

相同的代码工作对我的网站

The same code works on my website.

这是它返回到我,当我尝试做选择:

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

推荐答案

您没有向我们展示您的连接字符串着呢,从你的问题,但如果你看到不清楚仅此行为从Visual Studio中或还VS以外的区域运行的应用程序时。

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.

如果你碰巧使用SQL Server的快速 AttachDbFileName = somename.mdf 的办法,你运行Visual Studio中的应用程序时,观察此行为,然后继续阅读。

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.

整个的用户实例和 AttachDbFileName = 的方法是有缺陷的 - 在最好的!当在Visual Studio中运行你的应用程序,它会围绕密度纤维板文件被复制(从的App_Data 目录到输出目录 - 通常是 .\bin\debug - 在你的应用程序中运行)和最有可能的,您 INSERT 工作得很好 - 但你只是在看错密度纤维板最终文件

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!

如果你想坚持使用这种方法,然后尝试把一个断点上的 myConnection.Close()通话 - 然后检查与SQL Server管理Studio快速的密度纤维板文件 - 我几乎可以肯定你的数据是否有

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.

真正的解决方案在我看来,将


  1. 安装SQL Server Express(和你已经做到这一点无论如何)

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

安装SQL Server Management Studio中快速

install SQL Server Management Studio Express

SSMS快递,给它一个合理的名称(如 MyDatabase的

create your database in SSMS Express, give it a logical name (e.g. MyDatabase)

连接使用其逻辑的数据库名称为(当你在服务器上创建它交给) - 和周围不乱用物理数据库文件和用户实例。在这种情况下,您的连接字符串会是这样:

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天全站免登陆