以编程方式更改数据源(数据库) [英] Change data source (database) programmatically

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

问题描述

大家好。

我使用Visual Basic 2010 Express和Sql Server Compact制作基本程序来跟踪某些数据。

名为Mydata.sdf的数据库位于Exe所在的文件夹中,我的程序使用该数据库。还有一个名为Archive的文件夹,其中包含一个具有旧数据的Mydata.sdf。有时用户需要查看存档中的旧数据。所以我的程序必须开始使用Archive文件夹中的数据库。如何在运行时更改程序的数据源路径。我的意思是当用户点击Button1时,我的程序将开始使用...中的数据...存档/ Mydata.sdf。



我的项目设置页面的屏幕截图可能对您有所帮助。链接:

< img src =http://i.hizliresim.com/81dWLk.jpg>



我的尝试:



我看对于互联网,但找不到任何已在Visual Basic上编写的代码。

Hi everyone.
Im using Visual Basic 2010 Express and Sql Server Compact to make a basic program to follow some datas.
The database named "Mydata.sdf" is in the same folder where the Exe is and my program uses that database. And also there is a Folder named "Archive" that contains a "Mydata.sdf" which has old datas. Sometimes user needs to look at the old datas in archive. So my program must start to work with database thats in "Archive" folder. How can i change the data source path of my program at runtime. I mean when user clicks Button1 my program will start to use the data in ...Archive/Mydata.sdf.

Screenshot of my project's settings page may help you. Link:
<img src="http://i.hizliresim.com/81dWLk.jpg">

What I have tried:

I've look for internet but couldn't find any code that has written on Visual basic.

推荐答案

在某些时候,您的代码使用类似的东西连接到您的数据库;

At some point your code is connecting to your DB using something like;
conn = New SqlCeConnection(My.Settings.MyDataConnectionString)
conn.Open()



您需要将 My.Settings.MyDataConnectionString 替换为 My.Settings.MyDataConnectionString1 或您的应用程序中的名称设置。

你如何做到这一点取决于你,你需要确定自己应该如何在数据库之间进行更改,正如Richard Deeming昨天建议的那样



亲切的问候


You need to replace the My.Settings.MyDataConnectionString with My.Settings.MyDataConnectionString1 or whatever the name is in your Application Settings.
How you do this is up to you, you need to determine when it should be changed between the databases yourself as Richard Deeming advised yesterday

Kind Regards


我最终为这个问题创建了自己的代码。我想为使用VB2010和SQL Server Compact的人分享它,并希望更改活动表单的数据源。



I finally create my own code for this issue. I want to share it for the people who uses VB2010 and SQL Server Compact and want to change the data source for the active form.

Dim sConnectionString As String
sConnectionString = "Data Source=" & My.Computer.FileSystem.CurrentDirectory & "\Year2015\MyData.sdf"
TableAdapterManager.Connection.ConnectionString = sConnectionString





这将更改活动表格的数据来源。其他表单继续使用默认源



This will change your data source of active form. The other forms continues to use the default source


这篇关于以编程方式更改数据源(数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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