本文介绍了C# - 本地数据库连接字符串的格式(.sdf文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以直接使用我的winform应用程序连接到我的本地db文件.sdf。但是当我使用connstring时无法这样做。这就是我的尝试



1。使用app.config文件



i am able to connect to my local db file .sdf directly with my winform app. but unable to do so when i am using an connstring. here is what i have tried

1. using app.config file

var config =  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.ConnectionStrings["DEVID"].ConnectionString = textBox6.Text;
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
            SqlCeConnection con = new SqlCeConnection();
            string DEVID = ConfigurationManager.ConnectionStrings["DEVID"].ConnectionString;
            con.ConnectionString = DEVID; // getting error as "object reference not set"
//SqlCeConnection con = new SqlCeConnection(DEVID); // in my app.config file i set my connection string = "Data Source=C:\Users\john\Documents\Visual Studio 2010\Projects\AECCS\AECCS\Database1.sdf





2.使用直接字符串变量





2. using direct string variable

//SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\john\Documents\Visual Studio 2010\Projects\AECCS\AECCS\Database1.sdf"); // this is the actual conn string value
            string connstring1 = "\"Data Source=" + textBox6.Text + "\"";
            string connstring2 = "@"+connstring1;
            textBox2.Text = connstring2; // matches exactly with my actual connection string, but still not getting to connect with .sdf file
SqlCeConnection con = new SqlCeConnection(connstring2); // getting error as format of initialization string does not conform at index 0""





请帮我解决问题



我尝试过:



// SqlCeConnection con = new SqlCeConnection(@Data Source = C:\ Users\john\Documents\Visual Studio 2010 \\ \\Projects\AECCS\AECCS\Database1.sdf); //这是实际的conn字符串值

string connstring1 =\Data Source =+ textBox6.Text +\;

string connstring2 = @+ connstring1;

textBox2.Text = connstring2; //与我的实际连接字符串完全匹配,但仍然无法连接.sdf文件

SqlCeConnection con = new SqlCeConnection(connstring2); //因为初始化字符串的格式不符合索引0而得到错误



please help me out to solve the issue

What I have tried:

//SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\john\Documents\Visual Studio 2010\Projects\AECCS\AECCS\Database1.sdf"); // this is the actual conn string value
string connstring1 = "\"Data Source=" + textBox6.Text + "\"";
string connstring2 = "@"+connstring1;
textBox2.Text = connstring2; // matches exactly with my actual connection string, but still not getting to connect with .sdf file
SqlCeConnection con = new SqlCeConnection(connstring2); // getting error as "format of initialization string does not conform at index 0"

推荐答案

SqlCeConnection con = new SqlCeConnection(connstring1);


这篇关于C# - 本地数据库连接字符串的格式(.sdf文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 15:55