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

Tags:
Database connectivity
Database programming
SQL Server connectivity
SQL Server database
Visual Studio
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: 3  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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • 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
    69,160 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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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

Following