C# Connection String to Access & SQL Server 2005

Hello All;

Learning ASP.NET.

OK. I have searched around and found a lot of code that is so compiled with a bunch of other code that it is hard for a newby to understand.
So, for this post, I am asking for the following.

#1: Connection String for Access Database (2000 & 2007)
#2: Connection String for SQL Server 2005

Basically like in ASP Classic where you can use this.
(Code below)
How would this be written for C# ASP.NET.
(I am using C#, as it is used in the Upload script that I am currectly using, so I am sticking with it for the moment)

Thank You
Carrzkiss
Set objConn = CreateObject("ADODB.Connection")
'objConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
'Server.MapPath ("database1.accdb") & ";"
objConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\InetPub\wwwroot\get\database1.accdb;Persist Security Info=False;"
objConn.Open

Open in new window

LVL 31
Wayne BarronAuthor, Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:
0
Wayne BarronAuthor, Web DeveloperAuthor Commented:
I already know about these, and that is not what I asked for.
What I asked was the full thing in .NET C# Format.

What would this be in .NET C# Format
Set objConn = CreateObject("ADODB.Connection") 
'objConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _ 
'Server.MapPath ("database1.accdb") & ";" 
objConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\InetPub\wwwroot\get\database1.accdb;Persist Security Info=False;" 
objConn.Open

Open in new window

0
Om PrakashCommented:
Please check the code:
string path = "G:\\InetPub\\wwwroot\\get\\database1.accdb"
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';";
System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(cnString);
try
{
    cn.Open();
    MessageBox.Show("Connection is open");
    cn.Close();
}
catch (System.Exception e)
{
    MessageBox.Show("Failed to connect.  Reason" + System.Environment.NewLine + System.Environment.NewLine + e.Message);
}

Open in new window

0
Wayne BarronAuthor, Web DeveloperAuthor Commented:
Errors

CS0103: The name 'MessageBox' does not exist in the current context

I read where the MessageBox needed

using System.Windows.Forms;

But, then I got an error when trying to use it.

I changed it over to use

 {
            StatusLabel.Text = "Failed to connect.  Reason" + System.Environment.NewLine + System.Environment.NewLine + e.Message;
        }

Which of course run without issue, I am not a huge fan of MessageBoxes
That can get right annoying when something happens, A simple Textfield that shows that something has happened is better.

Complete code that works so far, if there is anything wrong with the way I have edited it, please let me know.

Carrzkiss
private void Page_Load(object sender, System.EventArgs ee)
{
string path = "G:\\InetPub\\wwwroot\\Testing\\NET\\1\\database1.accdb" ;
string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';"; 
System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(cnString); 
try 
{ 
    cn.Open(); 
    StatusLabel.Text = "Connection is open"; 
    cn.Close(); 
}
catch (System.Exception e) 
 { 
            StatusLabel.Text = "Failed to connect.  Reason" + System.Environment.NewLine + System.Environment.NewLine + e.Message; 
        } 
}

Open in new window

0
Wayne BarronAuthor, Web DeveloperAuthor Commented:
Another way of connecting to a database within a ASP.NET Project.

Enjoy.
Carrzkiss
OleDbConnection objConnection = null;
            OleDbCommand objCmd = null;
            String strConnection, strSQL;

            strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
            strConnection += @"Data Source=" + MapPath("Database1.mdb");

            objConnection = new OleDbConnection(strConnection);
            objConnection.ConnectionString = strConnection;

            objConnection.Open();
            strSQL = "INSERT INTO MegaPics(PicsPath)VALUES(?)";
            objCmd = new OleDbCommand(strSQL, objConnection);
            objCmd.Parameters.Add("@PicsPath", fileName);
            //objCmd.Parameters.Add("@PicsPath", RadUpload1.UploadedFiles[0].GetName());

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.