Sunday, March 11, 2012

cannot login

Well, I've tried every suggestion by helpful people in connecting to a database I created, to no avail.

I created a db, called, nodes10.mdf.
Test connection works.
Right now I get:

Cannot open database "nodes10" requested by the login. The login failed.
Login failed for user '<server>\<user>'.
Before you call me on my syntax, the problem is with logging in , not my insertString.

code:

SqlConnection conn = new SqlConnection("Data Source=NORBY11\\SQLExpress;Integrated Security=SSPI;Initial Catalog=nodes10;Connect Timeout=30;User Instance=True");

string ndeText=numberNode.Text;
string insertString = @."INSERT INTO table1(nodenames) VALUES(ndeText)";
SqlCommand cmd = new SqlCommand(insertString,conn);
//cmd.CommandText = insertString;

comboBox1.Items.Add(numberNode.Text);

try
{

conn.Open();

cmd.ExecuteNonQuery();

Also, are you supposed to be able to see my database in sqlexpress? like when you open up sqlserver config?

And why does my connection icon have a red x after I start my program?

Here are a couple questions:

Are you logged onto NORBY11 when you run this code? User Instances only support local connections.|||

yes, I'm logged into norby11.
So I've tried to use the attachdbfile syntax but the compiler doesn't like it. What should it be?
If I cut and paste the attachdb stuff from the properties window for the db, is that the correct way, because the compiler sure ain't buying it!

In the app.config file it says:

AttachDbFilename=|DataDirectory|\nodes10.mdf

HUH?|||Also, I don't know what this implies:

table1 (or whatever table number I'm fooling with at the time) is invalid object.
I got this one time while fooling around with different things in the connection string.
Does that mean no connection was made to the database, or is it that the table wasn't found, or that the table should exist someplace on my hard drive or ?|||

The AttachDBFilename syntax supports the use of |DataDirectory| when the database is in the same directory as the application. (Bold 'cause it's important.) If you put your database in a different directory, then you need to specify the full path to the database file.

Just for grins, is this a WinForm app or an ASP.NET web site your working in?

I got a small alarm bell when you mentioned copying the connection string out of the property box. Is this database embedded into your application? If the database is embedded into your application the connection string is automatically managed by VS so you don't need to open a connection manually.

If this is an ASP.NET application the issue could be with permissions since the user loging in is probably the ASP.NET user rather than your own user account. I'm not much of a web guy, but if this is the case, I can point you to a whole set of forums targeted at developing web applications on SQL Express.

Regards,

Mike Wachal
SQL Express team

-
Check out my tips for getting your answer faster and how to ask a good question: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=307712&SiteID=1

|||It's a WinForms app.

I believe it's called an embedded db. I created the database from within visual studio.

If I don't need to open the connection, why do I get an error on the line cmd.ExecuteNonQuery regarding opening a connection? --'connection property has not been initialized?'|||I have a couple more questions, if I could.

When I create a new data connection, then create a table within that db, should a table exist in the directory where my program is located? I notice in programs that I tried in VC#2005 book that tables appear in the directory. Of course, those programs didn't work either, and I can't connect to the db in them, or I get invalid object when I run them.

What a cluster.|||

Hi zenguitar,

As far as the need to connect, I'm talking about using DataSets in VS 2005. You'll be much better information about these by reading the VS documentation (http://msdn2.microsoft.com) and by hanging out in the .NET Data Access forum at http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=45&SiteID=1, but I'll try to give you the basics here.

There are a number of different ways to create a DataSet in VS, including adding a database into your project and creating a new DataSource from an existing database. The net of these actions, is that you end up in the DataSet Wizard and you can pick database objects to be exposed in the DataSet.

Once you have a DataSet, you can just drag items from the Data Sources pane onto your WinForm and have VS do all the magic of creating the objects needed to bind your data to your controls. VS also creates additional items such as a BindingSource and a TableAdapter and BindingNavigator to make you life easier. Using a DataSet also stores a ConnectionString for your database in the application settings and uses that connection string to automatically connect to your database to retreive or updated data based on your code against the DataSet. Exactly how the connection string is formed depneds on how you create the DataSet.

If you're goal is to place data bound controls on your forms, I suggest that explore the use of the DataSet as the way to do this since a bunch of the functionality is baked into VS for you.

Performing an operation such as running a Command object is still going to require that you make the connection yourself. Now you can write your connection string manually, you could store a connection string in the applicaiton settings or you could "steal" connection string from an existing Data Source that is already stored in your database. It's all really up to what your needs are.

None of this really gets to why you are getting a failed login error though. My guess is that you've still got a technology mix up between the user instance and the main instance and which one you're actually using.

Is your database nodes10 actually attached to SQL Express already? Do you access it via any other tools? (Which ones?) Does your user account actually have a Login on the server and a User account in the database?

As far as embedded databases, one way to get an embedded database is to create it via the Projects | Add New Items menu. One of the options is SQL Database, and this will actually put a database file directly in your project directory. It will also show up in Solution Explorer. If you don't see the database in Solution Explorer, then it isn't embedded. The whole idea of an embedded database is that the .mdf and .ldf files travel with the applicaiton and are attached at runtime to a User Instance. These are not suitable for an application that requires multi-user access becasue User Instances only support single user, local only access.

If you create a database using Server Explorer, it is not embedded, rather it is attached the main instance of the server and placed in the Data directory for the server. This is the way to go for multi-user access databases.

You can create DataSets against either of these databases.

As far as your final questions:

Tables don't exist independently of the database file. When you create a new table, it is stored in the database file in what ever directory that database was created in. Where the database resides is dependent on how it was created in VS as I've described above.

No comments:

Post a Comment