简体   繁体   English

使用app_data .mdf数据库进行操作

[英]Operate with app_data .mdf database

I am using Visual Studio 2013. Created Web Forms Project. 我正在使用Visual Studio2013。已创建Web窗体项目。 Added some stuff and needed database. 添加了一些东西和所需的数据库。 Also, added database using Add Item. 另外,使用“添加项目”添加数据库。 Added some stuff to a database. 向数据库添加了一些东西。 Use connection string from Web Config. 使用Web Config中的连接字符串。 When connection is made, i got some errors. 建立连接后,出现一些错误。

Here is my connection string > 这是我的连接字符串>

<connectionStrings>
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-mywebsite-20150813211505;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-mywebsite-20150813211505.mdf" />
  </connectionStrings>

C# code > C#代码>

SqlConnection con = new SqlConnection(@"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-mywebsite-20150813211505;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-mywebsite-20150813211505.mdf");
            SqlCommand cmd = new SqlCommand("select * from Users;");
            con.Open();
            DataSet ds = new DataSet(cmd, con);
            SqlDataAdapter da = new SqlDataAdapter();
            da.Fill(ds);
            con.Close();

here is the errors 这是错误

Error 1 错误1

The best overloaded method match for 'System.Data.DataSet.DataSet(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)' has some invalid arguments c:\\users***\\documents\\visual studio 2013\\Project\\Default.aspx.cs 30 26 mywebsite 'System.Data.DataSet.DataSet(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)'的最佳重载方法匹配具有一些无效参数c:\\ users *** \\ documents \\ visual studio 2013 \\ Project \\ Default.aspx.cs 30 26 mywebsite

Error 2 错误2

Argument 1: cannot convert from 'System.Data.SqlClient.SqlCommand' to 'System.Runtime.Serialization.SerializationInfo' c:\\users***\\documents\\visual studio 2013\\Projects\\Default.aspx.cs 30 38 Administration Website 参数1:无法从“ System.Data.SqlClient.SqlCommand”转换为“ System.Runtime.Serialization.SerializationInfo” c:\\ users *** \\ documents \\ visual studio 2013 \\ Projects \\ Default.aspx.cs 30 38管理网站

Error 3 错误3

Argument 2: cannot convert from 'System.Data.SqlClient.SqlConnection' to 'System.Runtime.Serialization.StreamingContext' c:\\users***\\documents\\visual studio 2013\\Projects\\Default.aspx.cs 30 43 mywebsite 参数2:无法从“ System.Data.SqlClient.SqlConnection”转换为“ System.Runtime.Serialization.StreamingContext” c:\\ users *** \\ documents \\ visual studio 2013 \\ Projects \\ Default.aspx.cs 30 43 mywebsite

When error is clicked it's focus on DataSet. 单击错误后,它将集中在DataSet上。

Help ?? 救命 ?? Some better idea to connect to my database ? 有更好的主意连接到我的数据库吗?

Thank you in advance ! 先感谢您 !

You have made some errors in initializing your DataSet and your SqlDataAdapter, the correct way is 您在初始化DataSet和SqlDataAdapter时犯了一些错误,正确的方法是

using(SqlConnection con = new SqlConnection(....))
{
    SqlCommand cmd = new SqlCommand("select * from Users;");
    cmd.Connection = con;
    con.Open();
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);
}

It is the SqlDataAdapter instance that needs the SqlCommand as parameter for one of its constructors, as you can easily spot on MSDN page . 它是SqlDataAdapter实例,需要SqlCommand作为其构造函数之一的参数,因为您可以在MSDN page上轻松发现。 Then the connection is bound the the SqlCommand.Connection property and you should enclose the creation of the SqlConnection in a using statement to be sure that it is closed and disposed at the end of the using block 然后,将连接绑定到SqlCommand.Connection属性,并且应该将SqlConnection的创建包含在using语句中 ,以确保已将其关闭并放在using块的末尾。

You can also remove two lines from the code above passing the connection directly to the constructor of the SqlCommand (again MSDN pages are a treasure of information) and remove the opening of the connection because, if the Adapter finds the connection closed, it opens and closes it automatically 您还可以从上面的代码中删除两行,将连接直接传递给SqlCommand的构造函数(同样, MSDN页面是一种信息宝库),并删除连接的打开位置,因为如果适配器发现连接已关闭,它将打开并自动关闭

About a different topic, you have correctly stored your connection string in the config file of your application. 关于另一个主题,您已将连接字符串正确存储在应用程序的配置文件中。 This allows you to easily change it (if the need arises) But you have used an hard coded connection string. 这使您可以轻松地更改它(如果需要),但是您使用了硬编码的连接字符串。 This should be changed to something like this 应该将其更改为这样的内容

 string connString2 = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

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

相关问题 未在App_Data中创建.mdf - The .mdf is not being created in App_Data 在App_data中找不到.mdf文件 - Cannot find .mdf file in App_data 为什么 mdf 文件没有出现在 App_Data 文件夹中? - Why is mdf file not appearing in the App_Data folder? ASP.NET 项目未在 app_data 中创建 .mdf 文件 - ASP.NET project not creating .mdf file in app_data 该进程无法访问文件&#39;C:Project \\ App_Data \\ Database.mdf&#39;,因为它正在被另一个进程使用 - The process cannot access the file 'C:Project\App_Data\Database.mdf' because it is being used by another process 如何访问 APP_DATA 中的数据库 - How to access Database which was in APP_DATA 如何让 Visual Studio 使用我的数据库,而不是位于 ASP.NET MVC 项目的 App_Data 文件夹中的 .mdf 文件? - How can I make Visual Studio use my database instead of the .mdf file located in App_Data folder in ASP.NET MVC project? GitHub 提交错误:权限被拒绝致命:无法处理路径 ~/App_Data/aspnet-MyProject.mdf - GitHub Commit Error: Permission denied fatal: Unable to process path ~/App_Data/aspnet-MyProject.mdf 该进程无法访问文件\\ App_Data \\ ASPNETDB.MDF&#39;,因为它正在被另一个进程使用。 - The process cannot access the file \App_Data\ASPNETDB.MDF' because it is being used by another process. SQL Server错误:50尝试将.mdf文件添加到App_Data时 - SQL Server error: 50 when trying to add .mdf file to App_Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM