• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

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

0
Wayne Barron
Asked:
Wayne Barron
  • 3
  • 2
2 Solutions
 
Om PrakashCommented:
0
 
Wayne BarronAuthor 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 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 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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now