Link to home
Start Free TrialLog in
Avatar of niceguy971
niceguy971

asked on

ASP.Net and ODBC

I can create ODBC Data Source

As described at http://www.depts.ttu.edu/ithelpcentral/solutions/odbcsetup.php

and use the below code in classic ASP :

Set objConn= Server.CreateObject("ADODB.Connection")

strConnection= "DSN= MyDSNName; Database= MyDBName; UID= MyUserID; PWD= MyPassword"

objConn.Open strConnection

strQuery="Select Name from MyTable"

Set objRS= objConn.Execute(strQuery)
--------------------------------------------------------
in .Net (C#) i can create SqlConnection like below:

SqlConnection conn = new SqlConnection("Data Source=DatabaseServer;Initial Catalog=Northwind;User ID=YourUserID;Password=YourPassword");

My question is:

How to modify the above string {SqlConnection conn = new SqlConnection(..)} to use ODBC Data Source MyDSNName?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Brian
Brian
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of niceguy971
niceguy971

ASKER

Hi! I'm trying to connect to SQL Server 2005... I would like to use ODBC Data Source MyDSNName ( Data Source MyDSNName has been created as described at http://www.depts.ttu.edu/ithelpcentral/solutions/odbcsetup.php)

to create new SqlConnection..so MyDSNName should be included in the below:

SqlConnection conn = new SqlConnection(....MyDSNName....)

What would be the proper way to do it?

Thanks
This is the way that I have always connected to SQL Server for all my projects. Of course you will hav to fill in your SQL Server name, Database Name, Username, and Password used for your Database.

For the web.config file, make sure you have the following setup (I used Mixed Authentication for SQL Server).

<connectionStrings>
    <add name="NameOfConnection" connectionString="Data Source=NameOfDBServer;Initial Catalog=YourDataBase;User ID=sa;Password=YourPassword" providerName="System.Data.SqlClient"/>
</connectionStrings>

For every page that will need to interact with SQL Server, make sure you have the following Namespaces declared at the top of your CodeBehind.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

Create SQL Connection:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NameOfConnection"].ConnectionString);

Create SQL Command:
SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "StoredProcedureName";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = conn;

Hope this helps!

Open in new window

it's a good example ..but it Does NOT answer my original question... Let me repeat my question:

I would like to use ODBC Data Source MyDSNName ( Data Source MyDSNName has been created as described at http://www.depts.ttu.edu/ithelpcentral/solutions/odbcsetup.php)

to create new SqlConnection..so MyDSNName should be included in the below:

SqlConnection conn = new SqlConnection(....MyDSNName....)

in classic asp: strConnection= "DSN= MyDSNName; Database= MyDBName; UID= MyUserID; PWD= MyPassword"

In Other words:

I would like to use the same DSN MyDSNName  {which has been created from Control Panel--->DataSources (ODBC)---> System DSN...and so on as described at at http://www.depts.ttu.edu/ithelpcentral/solutions/odbcsetup.php} to create new SqlConnection in C#.Net

What would be the proper syntax?
Why do you wish to create a connection to SQL Server that way? If you are connecting to SQL Server than use the SQL Server Connection Class along with it's Methods.
Not sure if this will work since I have never done it that way, but give this a shot and let me know if it works.

Modify your Connection in your web.config file to point to the name for your MyDSNName.

<add name="NameOfConnection" connectionString="Data Source=MyDSNName;Initial Catalog=YourDataBase;User ID=sa;Password=YourPassword" providerName="System.Data.SqlClient"/>
I am not at work right now, so I cannot give you specific examples from a real world example.  But, here is what I found for you.

private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection())
    {
        connection.ConnectionString = connectionString;

        connection.Open();

        Console.WriteLine("State: {0}", connection.State);
        Console.WriteLine("ConnectionString: {0}",
            connection.ConnectionString);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code, 
    // you can retrieve it from a configuration file.
    return "Data Source=MSSQL1;Initial Catalog=AdventureWorks;"
        + "Integrated Security=true;";
}

Open in new window

@Friman001:

Same thing above 36514900 but example in post 36514900 is easier and more widely used.
If need change GetConnectionString() to include the username and password.  On most SQL Servers, the username is sa.

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code, 
    // you can retrieve it from a configuration file.
    return "Data Source=MSSQL1;Initial Catalog=AdventureWorks;"
        + "Integrated Security=true; User ID=sa; Password=Password$1";
}

Open in new window

I see that now.  That was actually what I would have shown him if I was at work (using the web.config to save the connection string and accessing it that way!)  Sorry.
@niceguy971: Basically, your DNS will become your Datasource, or the address you use to connect to the SQL database.
SO:
static private string GetConnectionString()
{
    // To avoid storing the connection string in your code, 
    // you can retrieve it from a configuration file.
    return "Data Source=MyDSNName;Initial Catalog=AdventureWorks;"
        + "Integrated Security=true; User ID=sa; Password=Password$1";
}

Open in new window


You really just needed to plug in the name with which you use to connect to SQL Server (You can use my version or asp_net2's, just so long as you plug in your server name with which you use as the Data Source portion.)  Also, you could try looking into the tools in the ToolBox if you use Visual Studio for a dev environment.  They have TableAdaptors which do the whole process of creating and accessing data from a database for you!
Initial Catalog would be the Database that has all your tables in it, btw!
Hi Friman001! Your example below is good. Back to my original question: Can I create Sql connection
like this  SqlConnection conn = new SqlConnection( Data Source=MyDSNName;Initial Catalog=AdventureWorks; Integrated Security=true; User ID=sa; Password=Password$1 ) ??

Is the above syntax correct?? Will it work?

---
static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,
    // you can retrieve it from a configuration file.
    return "Data Source=MyDSNName;Initial Catalog=AdventureWorks;"
        + "Integrated Security=true; User ID=sa; Password=Password$1";
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks