Literals possibly fouling connectionstring

G

Guest

I'm trying to connect to an Sql Express database and I'm not sure if my
problem is with the C# string expression or the connection string itself.
The code was originally working fine when connecting to an online MSSQL
database. Now I need to modify the code to work with a local server. Here's
the code:


SqlConnection cn = new SQLConnection();
cn.ConnectionString = @ConnectString=@"Data
Source=.\SQLEXPRESS;AttachDbFilename=""C:\VS
Projects\Personal Utilities\TableClassBuilder\FTTestLocal.mdf"";Integrated
Security=True;User Instance=True";
cn.Open();

SqlCommand myCommand = new SqlCommand("Use FTTestLocal Create Table Asset
(AssetID int Identity Not Null Primary Key, AssetName varchar(150) Not Null,
Modified TimeStamp)", cn);

int ct = myCommand.ExecuteNonQuery();

When I run the application I get an error when the command is executed
stating "Could not locate entry in sysdatabases for database 'FTTestLocal'.
No entry found with that name. Make sure the name is entered correctly."

I also attempted to include the file extension .mdf in the SqlCommand. This
had no impact. The error message that appeared still only referrenced the
filename without the extension.

Thanks in advance for any help.
 
J

james.curran

OK, the
cn.ConnectionString = @ConnectString=@"
syntax looks a bit bizarre (I'll assume that you have a "string
@ConnectString;" somewhere).

I'd give it a try without the "User Instance=True".
 
G

Guest

I'm trying to connect to an Sql Express database and I'm not sure if my
problem is with the C# string expression or the connection string itself.
The code was originally working fine when connecting to an online MSSQL
database. Now I need to modify the code to work with a local server. Here's
the code:

SqlConnection cn = new SQLConnection();

cn.ConnectString=@"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\VS
Projects\Personal Utilities\TableClassBuilder\FTTestLocal.mdf"";Integrated
Security=True;User Instance=True";

cn.Open();

SqlCommand myCommand = new SqlCommand("Use FTTestLocal Create Table Asset
(AssetID int Identity Not Null Primary Key, AssetName varchar(150) Not Null,
Modified TimeStamp)", cn);

int ct = myCommand.ExecuteNonQuery();

When I run the application I get an error when the command is executed
stating "Could not locate entry in sysdatabases for database 'FTTestLocal'.
No entry found with that name. Make sure the name is entered correctly."

I also attempted to include the file extension .mdf in the SqlCommand. This
had no impact. The error message that appeared still only referrenced the
filename without the extension.

Thanks in advance for any help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top