如何在asp.net中与.sdf文件建立连接? [英] how to make connection with .sdf file in asp.net?

查看:271
本文介绍了如何在asp.net中与.sdf文件建立连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i已被赋予与.sdf文件建立连接的任务,但我熟悉.mdf文件,它直接是一个数据库文件。

i使用sqlconnection,sqlcommand和sqldatareader执行此任务,但是当我打开连接时出现网络相关错误。

请在这方面提供帮助。


i have been given a task to establish a connection with .sdf file,but I am familiar with .mdf file which is directly a database file.
i did this task using sqlconnection,sqlcommand,and sqldatareader but there is network related error when i open the connection.
please help in this regards.

推荐答案

您好,



连接.sdf文件需要进行一些更改。我使用的是Northwind数据库。首先,您必须在Web配置文件中修改connectionstring。你必须提到你正在使用什么类型的提供商。







Hello ,

Some changes are required to connect with .sdf file .here i am using "Northwind" Database . first of all you have to modify you connectionstring in your web config file . you must mention what type of Provider you are using.



<add name="NorthwindConnectionString" connectionString="Data Source=|DataDirectory|\Northwind.sdf" providerName="Microsoft.SqlServerCe.Client.3.5" />





在下一阶段,



在连接数据库之前,添加此命名空间。





in the next stage ,

before you connecting to your database ,add this namespace.

using System.Data.SqlServerCe



,因为您使用的是Sql Compact版本。



在这里,您必须使用


because you are using Sql Compact edition .

Here , you have to use

SqlCeConnection ,SqlCeDataAdapter

而不是

sqlconnection,sqlcommand,and sqldatareade

连接数据库。



to connect with database.

DataSet ds = new System.Data.DataSet();
       void Bind()
       {
           SqlCeConnection con = new SqlCeConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
           SqlCeDataAdapter da = new SqlCeDataAdapter("select p.[Product ID],p.[Product Name],c.[Category ID],c.[Category Name] from Products p inner join Categories c on p.[Category ID]=c.[Category ID]", con);
           da.Fill(ds);
      }





在这里,您将从Northwind数据库中获得一系列产品。



谢谢



here you will get a collection of Products from "Northwind" database .

thanks


连接到你必须使用的sdf文件System.Data.SqlClient命名空间和connectionstring就像

Data Source = sdffilepath; Password = pass; File Mode = Exclusive; Persist Security Info = False;
to connect to sdf files you have to use System.Data.SqlClient namespace and connectionstring would be like
"Data Source = sdffilepath; Password = pass; File Mode=Exclusive; Persist Security Info=False;


这篇关于如何在asp.net中与.sdf文件建立连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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