Mike
Mike

Reputation: 435

Windows Form Application and SQL Server

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

Upvotes: 0

Views: 6311

Answers (2)

surfmuggle
surfmuggle

Reputation: 5954

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

Upvotes: 2

Ehsan Mirsaeedi
Ehsan Mirsaeedi

Reputation: 7592

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.

Upvotes: 0

Related Questions