Sunday, March 25, 2012

Connecting to SQL Express from C# via connection string

I created a SQL Express database from within Vistual Studio. It had a
property ConnectionString, so I grabbed that value and attempted to use it i
n
code. (I did add an @. at the beginning and doubled the literal double quote
symbols.) The code that assigns the connection string and opens the
connection then looked like the following:
SqlConnection cn = new SQLConnection();
cn.COnnectionString = @."Data Source=
.\SQLEXPRESS;AttachDBFilename=""C:\MySolution\MyData.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."
Thanks in advance for any help.Your connection string is attaching one database named
C:\MySolution\MyData.mdf and then you are trying to create a table in a
database named FTTestLocal. Either your connection string is wrong or you
are trying to use the wrong database. Perhaps you should explain what
you're trying to do so we can tell which database is appropriate.
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Steve Wash" <SteveWash@.discussions.microsoft.com> wrote in message
news:E426F5B2-E8E0-4D54-AAE2-949088D2A673@.microsoft.com...
>I created a SQL Express database from within Vistual Studio. It had a
> property ConnectionString, so I grabbed that value and attempted to use it
> in
> code. (I did add an @. at the beginning and doubled the literal double
> quote
> symbols.) The code that assigns the connection string and opens the
> connection then looked like the following:
> SqlConnection cn = new SQLConnection();
> cn.COnnectionString = @."Data Source=
> .\SQLEXPRESS;AttachDBFilename=""C:\MySolution\MyData.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."
> Thanks in advance for any help.
>|||Thanks for your quick review of my problem. I apologize that I made a
type-o, and your solution addresses the type-o. In trying to simplify my
example, I removed a lot of cryptic pathing information, but accidently left
in the "FTTestLocal". The actual code is
ConnectString=@."Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\VS
Projects\Personal Utilities\TableClassBuilder\FTTestLocal.mdf"";Integrated
Security=True;User Instance=True";
The SQL command is "Use FTTestLocal Create Table Asset (AssetID int Identity
Not Null Primary Key, AssetName varchar(150) Not Null, Modified TimeStamp)"
I've also tried
"Use FTTestLocal.mdf Create Table Asset (AssetID int Identity Not Null
Primary Key, AssetName varchar(150) Not Null, Modified TimeStamp)".
Interestingly, the error message did not change at all. It did not seem to
pick up the .mdf in any way.
As for what I'm trying to accomplish, I'm attempting to modify existing
code. Currently the code attaches to an online server running MS SQL 2005
using the previously referenced code. I need to modify it to work as close
as possible to the same way but to instead attach to SQL Express. This way
I
can continue to develop without always needing to go online.|||Try running
SELECT name FROM sys.databases
to get the name of the database. It is probably:
C:\VSProjects\Personal Utilities\TableClassBuilder\FTTestLocal.mdf
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Steve Wash" <SteveWash@.discussions.microsoft.com> wrote in message
news:C92EB4DD-7189-487B-9BE9-03BD64B68ACA@.microsoft.com...
> Thanks for your quick review of my problem. I apologize that I made a
> type-o, and your solution addresses the type-o. In trying to simplify my
> example, I removed a lot of cryptic pathing information, but accidently
> left
> in the "FTTestLocal". The actual code is
> ConnectString=@."Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\VS
> Projects\Personal Utilities\TableClassBuilder\FTTestLocal.mdf"";Integrated
> Security=True;User Instance=True";
> The SQL command is "Use FTTestLocal Create Table Asset (AssetID int
> Identity
> Not Null Primary Key, AssetName varchar(150) Not Null, Modified
> TimeStamp)"
> I've also tried
> "Use FTTestLocal.mdf Create Table Asset (AssetID int Identity Not Null
> Primary Key, AssetName varchar(150) Not Null, Modified TimeStamp)".
> Interestingly, the error message did not change at all. It did not seem
> to
> pick up the .mdf in any way.
>
> As for what I'm trying to accomplish, I'm attempting to modify existing
> code. Currently the code attaches to an online server running MS SQL 2005
> using the previously referenced code. I need to modify it to work as
> close
> as possible to the same way but to instead attach to SQL Express. This
> way I
> can continue to develop without always needing to go online.|||That was it. Thanks
"Roger Wolter[MSFT]" wrote:

> Try running
> SELECT name FROM sys.databases
> to get the name of the database. It is probably:
> C:\VSProjects\Personal Utilities\TableClassBuilder\FTTestLocal.mdf
> --
> This posting is provided "AS IS" with no warranties, and confers no rights
.
> Use of included script samples are subject to the terms specified at
> http://www.microsoft.com/info/cpyright.htm
> "Steve Wash" <SteveWash@.discussions.microsoft.com> wrote in message
> news:C92EB4DD-7189-487B-9BE9-03BD64B68ACA@.microsoft.com...
>
>

No comments:

Post a Comment