2 Replies - 6509 Views - Last Post: 07 November 2010 - 07:32 AM Rate Topic: -----

#1 jerryjliu98   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-November 10

Using Microsoft SQL Compact 3.5 with Visual Studio C# 2008

Posted 06 November 2010 - 11:01 AM

I'm a novice programmer at C#, and recently I have been working on trying to fetch data from the Microsoft SQL Compact 3.5 database bundled with Visual Studio Express 2008 for use in my Windows forms application project(for fun). Basically, I store vocab words in a WordList table, along with its tense and definition, and I simply want to first display these items in a textbox in my application.(I only have 3 words so far).

I have followed the MSDN tutorial on how to set up a connection to this database, and the database shows up in Visual Studio as a Database Connection. Also, through this connection I have generated a vocabDataSet. Here, however, I am lost.

From what I have read on the internet, you have to set up a connection in your code, like something along the lines of
static string connectionString = "Initial Catalog=vocab;Data Source=D:\\myfolders\\vocab.sdf;Integrated Security=SSPI;";
static SqlConnection connection = new SqlConnection(connectionString); 

Yet the program cannot seem to establish a connection with the SQL database when I run it. I'm certain the connectionString is the problem. What exactly am I supposed to put for the connection string? I'm confused, because do I need to establish a connection at all, since I already set up a database connection through Data->Add New Data Source? If not, what do I need to do then in order to get data from the database?

This problem may sound really dumb or be confusing to understand. Feel free to ask any clarification questions. Any reply would be appreciated.

MOD EDIT: Moved from C# Tutorials

This post has been edited by JackOfAllTrades: 06 November 2010 - 11:07 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Using Microsoft SQL Compact 3.5 with Visual Studio C# 2008

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1535
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Using Microsoft SQL Compact 3.5 with Visual Studio C# 2008

Posted 07 November 2010 - 05:16 AM

Your connection string should be this...

static string connectionString = "Data Source=D:\\myfolders\\vocab.sdf;Persist Security Info=False;";

Was This Post Helpful? 0
  • +
  • -

#3 BrairMoss   User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 81
  • Joined: 13-April 10

Re: Using Microsoft SQL Compact 3.5 with Visual Studio C# 2008

Posted 07 November 2010 - 07:32 AM

Well since you said you used the GUI Wizard to set up a data connection, you can even get away without typing the data connection string that way, and refer directly to the connection it set up.

To do this it would look something like: (Note: I have my own separate class in a library to connect to the DataBase all the time.)

string connectionString = global::(Namespace).Properties.Settings.Default.(Connection String named in GUI);
static SqlConnection connection = new SqlConnection(ConnectionString);

Using that line for connectionString can avoid lots of typos, and worrying about escape characters.

Of course if you use an internal Class to connect to the database, the SqlConnection is declared class level, and then in the constructor for the class, you would say the connection.ConnectionString is the global:: line.

Although the person above me did fix the line, it is always nice to know additional ways to do something I find. Hope it helps.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1