0

UPDATE 2

I fixed the error by adding System.Configuration to the reference file. I had already added it to my class with using System.Configuration;
I found the answer here: The name 'ConfigurationManager' does not exist in the current context

Any further issues will be addressed in a new question.

Original(ish) Post

I have Microsoft SQL Server Management Studio. In the studio I created a database called Logbook that exists locally on my computer.

I have also created a user interface using a Windows forms application in VS C# Express. I would like to connect it to my logbook database in order to update, select, delete, and insert entries into the database through the UI.

However, I cannot figure out how connect the two. I have played around with the "Add new data source" wizard to no avail. I also cannot find anything helpful in the MSDN or other tutorials online.

UPDATE

I created a new database and project to work with until I figure out how to properly do this so I don't accidently break anything in my project.
The Schema is:

Student(sid: int, fname: char(10), lname: char(10), age:int, major:char(10))
Course(cid:int, desc:char(50), dept:char(10))
Enrolled(sid:int, cid:int, quarter:char(10), grade:char(10))

Here is the connection string generated by the "Add Data Source" wizard

<add name="boathouseLogConnectionString"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=&quot;C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\boathouseLog.mdf&quot;;Integrated Security=True;Connect Timeout=30;User Instance=True"
        providerName="System.Data.SqlClient" />

I have successfully gotten my database into the project. However, when I try running a stored procedure I get this error Argument Exception was unhandled. Format of the initialization string does not conform to specification starting at index 0.

The error occurs on line 2.

        DataTable dt = new DataTable();
        SqlConnection sqlConn = new SqlConnection(selectedConnectionString);
        SqlCommand myCommand = new SqlCommand("proc_getMember", sqlConn);
        myCommand.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(myCommand); 

the selectedConnectionString variable is set to boathouseLogConnectionString

Community
  • 1
  • 1
Mike
  • 437
  • 3
  • 8
  • 18
  • How many and what kind of parameters does the stored procedure `proc_getMember` have? Where is the part in your code where you add the parameters to the sql Command. Something like this: `command.Parameters.Add("@ID", SqlDbType.Int);` and `command.Parameters["@ID"].Value = customerID;` see here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.100).aspx – surfmuggle Nov 07 '12 at 10:41

2 Answers2

2

Initial Hints

If you want to see some examples for windows forms connecting to a database i can recommend the forms over data video series. Video 2 is about how to connect to a database.

In the app.config file you can put your connection data

<connectionStrings>
    <add name="OrderManager.My.MySettings.OMSConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\OMS.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

To be able to help you better i need more details. If you connect in management studio you can take a look at your connections properties. What are the values here and so on.

Update

There is another question with a similar problem. May be you are running user instances. Please add more details:

  • add information about your connection string (i am assuming your are using file attached connnections with a local data file)
  • where do you click on your database? Are you clicking on the mdf file or are you using management studio or visual studio
Community
  • 1
  • 1
surfmuggle
  • 5,527
  • 7
  • 48
  • 77
  • I tried to add a Data source like the video wanted, but when i am adding a connection and I click on my database it returns this message _This file is in use. Enter a new name or close the file that's open in another program._ I don't know why its saying this however, because I dont have anything else open. – Mike Nov 04 '12 at 00:19
  • I haven't actually solved my problem yet. The reason I posted the other question it was a new error i was getting and i needed to get past it in order to continue doing what your tutorial wanted. Since then I switched to a different project I am working on that is due sooner – Mike Nov 05 '12 at 03:35
  • Above you write that you get this error message: "this file is in use. Enter a new name or close the file that's open in another program". Isn't this the same error message that you had in your other question. Have you tried the steps provided in the other answer? I am assuming that you connect to the database logbook as a mdf file instead of over the sql server. But to help you better it would be very helpfull if you provide more details. You said you are using the wiziard to no avail - what does the wizard create, what is in your app.config file and so on. – surfmuggle Nov 05 '12 at 08:04
  • It is the same error I posted about in the other question. The reason I did that was it was only a comment on here and I wasnt able to keep going until I resolved that error and I didnt know when/if you'd reply to the comment. If that wasnt the right thing to do, I am sorry. Fortuantely, a poster was able to provide a solution for getting fixing that error by unattaching the database in Server Studio. I will post an update to my question with the rest of the information to what is happening when I am trying to connect the database and Windows form project. – Mike Nov 05 '12 at 21:39
  • I have updated my question with the new error i am receiving. `Argument Exception was unhandled. Format of the initialization string does not conform to specification starting at index 0.` any help you could provide would be greatly appreciated – Mike Nov 07 '12 at 00:11
0

There is two major ways, you can use Entity Framework or ADO.NET foundation. For simple tasks you can use ado.net features. at first you have to create SqlConnection Object and pass your ConnectionString to the constructor. Second you have to use SqlCommand Object and set your desired Command Text for the command. and at last you have to Execute the Command. there are several ways for execution :

  1. ExecuteNonQuery For Updates, Deletes and Inserts commands

  2. ExuecuteReader For Select commands, this methods returns a SqlDataReader which you can get your result from it.

Ehsan Mirsaeedi
  • 6,924
  • 1
  • 41
  • 46