How to connect my application using Visual Studio 2005 to a SQL Server database?

Tags:
Database connectivity
Database programming
Microsoft Visual Studio
SQL Server connectivity
SQL Server database
Visual Studio 2005
how to connect my application using visual studio 2005 to a database using sql server?

Answer Wiki

Thanks. We'll let you know when a new response is added.

You need to create a SQLConnection object as well as a SQLDataReader object as well as a SQLCommand object and use them to execute the SQL Statement in the SQLCommand object against the SQLCOnnection object and put the output into the SQLDataReader object.

Here are some examples:

Visual Basic .NET

Dim cnn As SqlConnection = New SqlConnection("server=(local);" & _
"database=pubs;Integrated Security=SSPI")
Dim cmd As SqlCommand = New SqlCommand("select * from authors", cnn)

cnn.Open()
MyRepeater.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
MyRepeater.DataBind()

Visual C# .NET

SqlConnection cnn = new SqlConnection("server=(local);
database=pubs;Integrated Security=SSPI");
SqlCommand cmd = new SqlCommand("select * from authors", cnn);

cnn.Open();
MyRepeater.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
MyRepeater.DataBind();

Usind DataSets:

Visual Basic .NET

Dim cnn As SqlConnection = New SqlConnection("server=(local);" & _
"database=pubs;Integrated Security=SSPI")
Dim cmd As SqlDataAdapter = New SqlDataAdapter("select * from authors", cnn)
Dim ds As DataSet = New DataSet()
cmd.Fill(ds)
MyRepeater.DataSource = ds
MyRepeater.DataBind()

Visual C# .NET

SqlConnection cnn = new SqlConnection("server=(local);
database=pubs;Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("select * from authors", cnn);
DataSet ds = new DataSet();
da.Fill(ds);
MyRepeater.DataSource = ds;
MyRepeater.DataBind();

Discuss This Question: 9  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Shaz
    Thanks for giving me solution,But it is showing error that MyRepeater is not declared how to declare it.Iam new to vb.net plz cooperate it Thanking u, Shazia.
    255 pointsBadges:
    report
  • Pr09
    Repeater Control The "MyRepeater" instance is an example of carlosdl's implementation of the Repeater class. Essentially all of the examples above, minus the calls to MyRepeater, are how you connect to the db, execute T-SQL commands, and get datasets back. He just uses a Repeater to handle the data, you could attach it to a listbox (or any number of other controls) if you so choose. Using VS2005 you can also use the SHIFT+ALT+D to open the datasources window and add your database. From there you can drag and drop columns and tables to your forms and they will be automatically databound.
    70 pointsBadges:
    report
  • carlosdl
    You can find some other examples here: Insert data using SqlCommand
    84,310 pointsBadges:
    report
  • sathyal
    I have doubt on how to connection establishment. While creating it need server name I don't know how to rectify it.
    20 pointsBadges:
    report
  • Subhendu Sen
    @sathyal, in Visual Studio, what language are you using now, Visual Basic or C#. More information can help to get correct replies. Or see the answer that can help you.
    86,760 pointsBadges:
    report
  • ToddN2000
    @sathyal: If you do not know the server name, I'd talk to the IT department if this is an office environment. They should be able to provide the info you are looking for. If this is related to a personal / home set-up more details are needed.
    82,455 pointsBadges:
    report
  • sathyal
    Sorry, Im not mention above whats the language used. Now im preparing for my project using c#.
      I think the server is related to personal. For that what can I do sir.

    20 pointsBadges:
    report
  • ToddN2000
    @sathyal: What version numbers of the software are you using or is it   Visual Studio Express and SQL Server Express?  To access a local database use the Visual C# examples listed above. IF you still have problems, post your code and any error messages so we can better assist.
    82,455 pointsBadges:
    report
  • Subhendu Sen
    What did you mean by 'I think', are you not sure about that on which computer the server is installed and are you a client side/server side. Assuming this is a standalone system. And also assuming that you are in a learning process. You can check the answer where it is clearly described. Or if you wrote some code, please come back with this one.
    86,760 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: