943,550 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 296822
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 8th, 2005
0

How do you connect to a sql database using C#

Expand Post »
Hello everyone,

I was wondering if anyone could tell me how to connect and query sql databases in c#.

Thanks in advance
Similar Threads
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Apr 9th, 2005
0

Re: How do you connect to a sql database using C#

Umm, ok. I will give you some sample code, but you should really consider checking out the tutorials here at Daniweb, or C# websites with tutorials. The principles are the same no matter what language, the syntax is the only thing that differs. And google searches are a really good place to start.

Sample - as a console application in C#

using System;
using System.Data.SqlClient;

namespace ConsoleCSharp
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class DataReader_SQL
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			//
			// TODO: Add code to start application here
			//
			try
			{
		    	SqlConnection thisConnection = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=192.168.0.100,1433;database=Northwind;User id=Paladine;Password=;");
				thisConnection.Open();
		 	SqlCommand thisCommand = thisConnection.CreateCommand();
		 	thisCommand.CommandText = "SELECT CustomerID, CompanyName FROM Customers";
			 SqlDataReader thisReader = thisCommand.ExecuteReader();
				while (thisReader.Read())
				{
		 		Console.WriteLine("\t{0}\t{1}", thisReader["CustomerID"], thisReader["CompanyName"]);
					}
				thisReader.Close();
				thisConnection.Close();

			}
			catch (SqlException e)
			{
				Console.WriteLine(e.Message);
			}
			
		}
	}
}

** NOTE **
If you are using Visual Studio, rather than the commandline compiler, use CTRL + F5 to compile, as that will put a pause in the Console app, and keep it open for you to see the results.

Hope this helps.
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003
Apr 10th, 2005
0

Re: How do you connect to a sql database using C#

how can you use the data to test conditions, not just write out data that you want.
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Apr 11th, 2005
0

Re: How do you connect to a sql database using C#

Here is an example of the principles of ADO.Net

pseudocode
C# Syntax (Toggle Plain Text)
  1. ...
  2. Sqldataadapter custAdapter = new SqlDataAdapter("SELECT * FROM Customers", thisConnection);
  3. custAdapter.Fill(thisDataSet, "Customers");
  4. foreach (DataRow custRow in thisDataSet.Tables["CustomerID"].Rows)
  5. {
  6. if (custRow["City"].ToString() == "Madrid")
  7. {
  8. Console.WriteLine("Customer ID: " + custRow["CustomerID"]);
  9. }
  10. }


Hope this helps.
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003
Apr 11th, 2005
0

Re: How do you connect to a sql database using C#

thanks this was a lot of help
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Apr 12th, 2005
0

Re: How do you connect to a sql database using C#

Glad I could help.

If I ever get a free mo...I will try to post some C# Tutorials
Team Colleague
Reputation Points: 211
Solved Threads: 27
Master Poster
Paladine is offline Offline
793 posts
since Feb 2003
Dec 4th, 2007
0

Re: How do you connect to a sql database using C#

Hi, I'm trying to gain access to a postGres database. I've tried to use Npgsql but I get an error with the Mono.Security.dll file (the version is incorrect > but I can't find v2). So I thought I'd give the above method a try. I was just wondering in the statement:
SqlConnection thisConnection = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=192.168.0.100,1433;database=Northwind;User id=Paladine;Password=;");
What does the 'Network Library=DBMSSOCN' refer to? And would it change if I'm using postGres? I'll try it out when I get to work tomorrow but just thought I'd ask.

Thanks!
Last edited by PoovenM; Dec 4th, 2007 at 10:58 am.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Dec 5th, 2007
0

Re: How do you connect to a sql database using C#

I've tested the above connection and there's an error in the detection of a SQLServer... which makes sense since I'm using the wrong provider... I think that 'Network Library=DBMSSOCN' specifies the provider? And a provider allows you to connect to the require database server... but I'm not sure. Regardless, I'm still unsure of how to connect to my PostGres database. Can some help please
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Dec 5th, 2007
0

Re: How do you connect to a sql database using C#

Well I've finally figured out how to get Ngpsql to work!! I downloaded the beta version and that came with the correct version of the Mono.Security.dll file. A piece of advice to those that plan to use it, perhaps you'd just want to copy the Mono.Security.dll file to the /bin/Debug and /bin/Release directories of your project instead of adding it as a reference.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
146 posts
since Aug 2006
Aug 6th, 2008
0

Re: How do you connect to a sql database using C#

can anyone please specify the concept of
SqlConnection thisConnection = new SqlConnection(@"Network Library=DBMSSOCN;Data Source=192.168.0.100,1433;database=Northwind;User id=Paladine;Password=;");

Thanks in Advance
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arsalanu1 is offline Offline
1 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C# Forum Timeline: how to connect the module with the code in a project in C #
Next Thread in C# Forum Timeline: COM Add-in for office, install problem.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC