View Single Post
  #15 (permalink)  
Old 07-02-2008, 06:50 PM
Pancrazio Pancrazio is offline
Newbie
 
Join Date: Jun 2008
Posts: 17
Credits: 0
Rep Power: 2
Pancrazio is on a distinguished road
Default Re: linking SQL database with C# problem

I'm not an expert and it's been more than a year since I've used C# (needed it for school) but if I remember correctly it goes something like this using SQL server:

using System.Data.SqlClient; //Namespace

SqlConnection conn = new SqlConnection(
"server=myservername;database=mydatabasename;uid=m yusername;pwd=mypassword"); /* defining a SQL connection named 'conn' with the correct servername, databasename, userid and password. The thing between brackets is the connection string, there is a whole website somewhere about nothing but the connectionstring. */
conn.Open(); // open the connection
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select x from x where x";//Using your SQL command
cmd.Connection = conn;
string Result = (string)cmd.ExecuteScalar(); //Put the result in a string named Result
Console.Writeline(Result); //Writes it in the console
conn.Close(); // close the connection


For multiple results you can use SqlDataReader.

The framework database programming classes are referred to as ADO.NET . Look ok the internet for ADO.NET tutorials. Programming is all about managing and manipulating data, so if it's your ambition to develop professional web applications using C# and .NET for, you need to learn this stuff.

If you don't know how to use SQL commands, you should learn that too.
SQL Tutorial - Learn SQL is the website I learned it from.

Last edited by Pancrazio; 07-02-2008 at 07:42 PM. Reason: mistake
Reply With Quote

Sponsored Links