| | |
How do you connect to a sql database using C#
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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#
** 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.
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.
Here is an example of the principles of ADO.Net
pseudocode
Hope this helps.
pseudocode
C# Syntax (Toggle Plain Text)
... Sqldataadapter custAdapter = new SqlDataAdapter("SELECT * FROM Customers", thisConnection); custAdapter.Fill(thisDataSet, "Customers"); foreach (DataRow custRow in thisDataSet.Tables["CustomerID"].Rows) { if (custRow["City"].ToString() == "Madrid") { Console.WriteLine("Customer ID: " + custRow["CustomerID"]); } }
Hope this helps.
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
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:
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!
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.
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
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
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
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.
![]() |
Similar Threads
- Update SQL database automatically using VB6 (Visual Basic 4 / 5 / 6)
- Connection to sql database in c# (ASP.NET)
- How to connect sql database in asp.net using VB? (ASP.NET)
- Connecting to database (VB.NET)
- Help with Roles Stored in SQL database (ASP.NET)
- Snyc'n Local SQL database online (MS SQL)
Other Threads in the C# Forum
- Previous Thread: Check if record exists in MSA using C#
- Next Thread: C# Lists - Beginner Help (XNA)
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml





