本地数据库 [英] Local Database

查看:88
本文介绍了本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c#中,我决定用本地SQL数据库代替以前使用Access或Excel DB服务的以前的程序.
我想在本地使用此数据库,但我不确定在网络上使用它.
但是有一些问题.
我编写了一个程序,并通过以下方式添加了本地数据库:
解决方案资源管理器>右键点击项目名称>添加>新商品
然后选择本地数据库"并添加一个名称为 Database1.sdf
的数据库
然后,我添加了一个带有两个字段的表格.
另外,该类已自动添加到我的程序中: Database1DataSet
在我的代码中,我写道:

In c#, I decided to substitute local SQL database for my previous programs used Access or Excel DB services before.
I want to use this database locally and I do not decide on using it in web.
But there are some problems.
I made a program and add a local database through:
Solution Explorer> right click on name of project> Add > New Item
then select "Local Database" and add a database with name: Database1.sdf

Then I added a tabel with two fields.
Also This class was added to my program automaticly: Database1DataSet
In my code I wrote:

        Database1DataSet ds = new Database1DataSet();
        SqlConnection cn;
        SqlDataAdapter da;
        DataRow dr;
        SqlCommandBuilder cb;
        bool newMode;
        const string TableName = "Users";

        public Form1()
        {
            InitializeComponent();
            InitalizeDatabase();
        }

        private void InitalizeDatabase()
        {
            newMode = false;
            string strSql = "SELECT * FROM " + TableName;
            cn = new SqlConnection("Data Source=(local);Initial 
Catalog=Database1;Integrated Security=True;Connection Timeout=10");
            da = new SqlDataAdapter(strSql, cn);
            cn.Open();
            da.Fill(ds, TableName);
            //////////
            textBox_UserName.DataBindings.Add(
                new Binding("Text", ds, TableName + ".UserName"));
            textBox_Password.DataBindings.Add(
                new Binding("Text", ds, TableName + ".Password"));
            dataGrid1.DataBindings.Clear();
            dataGrid1.DataBindings.Add(
                new Binding("DataSource", ds, TableName));
            //////////
            cn.Close();
        }


编译没有错误,但是每次我在以下行运行程序时,都会出现运行时错误:


There was no error in compiling, but a runtime error appears each time I run the program at the line:

cn.Open();


:confused:异常:
建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接. (提供者:命名管道提供者,错误:40-无法打开与SQL Server的连接)

我尝试了许多字符串连接,但总是出现以下代码之一的异常:0、26、40
它骚扰了我.我尚未安装SQL,但是默认情况下Windows7中提供了SQL服务.然后我手动激活了所有它们:


尽管有些程序员说不需要安装SQL,但我感到头疼.
有没有人帮我解决这个问题?
非常感谢.


:confused:Exception:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

I tried many string connections, and always an exception was appeared with one of these codes: 0, 26, 40
It harassed me. I have not installed SQL but SQL services are avalable in my Windows7 by default. and I actived all of them manually:


Although some of prograsmmers say no SQL installation is needed but I got doughbtful.
Is there anyone help me how to deal with such a problem?
Thx very much.

推荐答案

如果您没有确认是否存在一个名为SQLExpress的命名实例,那么我不明白为什么希望它能正常工作.下载并安装SQL Server Express Edition,然后从那里开始工作.

另外,不要在答案区域发表评论,这只会使事情变得混乱.我们回复时看不到您发布的内容.编辑您的帖子或使用底部的区域,您可以在该区域使用一般论坛.
If you didn''t confirm that there''s a named instance called SQLExpress, then I don''t see why you''d expect this to work. Download and install SQL Server Express Edition, then work from there.

Also, DON''T post comments in the answers area, it just messes things up. It''s not possible to see what you posted while we reply. Edit your post or use the area down the bottom, where there''s a general forum you can use.


我遇到了完全相同的问题.显然,Microsoft更改了.NET与SQL Express的配合方式.现在,如果要连接到.sdf文件,则需要使用SqlCeConnection,SqlCeCommand,CqlCeReader等,而不是SqlConnection.

我希望他们在引入这样的更改时会提供有意义的例外.

维克多,
软件开发人员
免费图像转换器

翻转我的照片
I ran into exactly the same problem. Apparently Microsoft changed how .NET works with SQL Express. Now if you want to connect to .sdf file, you need to use SqlCeConnection, SqlCeCommand, CqlCeReader, etc. instead of SqlConnection.

I wish they would provide a meaningful exception, when they introduce changes like this.

Victor,
Software Developer
Free Image Converter

Flip My Photos


尝试
Data Source =".\Database1.sdf";


这是如果您的数据库直接位于debug或release文件夹中


This is if your database is directly in debug or release folder


这篇关于本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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