Solved

ASP.Net and ODBC

Posted on 2011-09-09
15
376 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:niceguy971
  • 6
  • 5
  • 4
15 Comments
 
LVL 4

Accepted Solution

by:
asp_net2 earned 250 total points
ID: 36514837
Hii niceguy971,

What Database are you trying to connect to using ODBC?

Let me know before I post more examples.
0
 

Author Comment

by:niceguy971
ID: 36514871
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
0
 
LVL 4

Expert Comment

by:asp_net2
ID: 36514900
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

0
 

Author Comment

by:niceguy971
ID: 36514945
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?
0
 
LVL 4

Expert Comment

by:asp_net2
ID: 36514952
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.
0
 
LVL 4

Expert Comment

by:asp_net2
ID: 36514958
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"/>
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36515027
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

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:asp_net2
ID: 36515043
@Friman001:

Same thing above 36514900 but example in post 36514900 is easier and more widely used.
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36515051
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

0
 
LVL 4

Expert Comment

by:Friman001
ID: 36515058
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.
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36515066
@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!
0
 
LVL 4

Expert Comment

by:Friman001
ID: 36515068
Initial Catalog would be the Database that has all your tables in it, btw!
0
 

Author Comment

by:niceguy971
ID: 36515417
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";
}
0
 
LVL 4

Assisted Solution

by:Friman001
Friman001 earned 250 total points
ID: 36516311
You still need the quotes.

SqlConnection conn = new SqlConnection("Data Source=MyDSNName;Initial Catalog=AdventureWorks; Integrated Security=true; User ID=sa; Password=Password$1");

Open in new window


The part within the new SqlConnection(...) is called the connection string, so it needs to be in quotes, since it is a string!
0
 

Author Closing Comment

by:niceguy971
ID: 36532944
Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now