[.net] C# connection to a db

Started by
12 comments, last by capn_midnight 16 years, 4 months ago
I'm having problems connecting to a database. The code I'm using is code I found in a tutorial but I changed the database name on the Initial Catalog line and the table on the query but some reason I keep getting an error at conn.open(). Am I missing something?

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

/// <summary>
/// Demonstrates how to work with SqlConnection objects
/// </summary>
class SqlConnectionDemo
{
    static void Main()
    {
        // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Grave Consequences;Integrated Security=SSPI");

        SqlDataReader rdr = null;

        try
        {
            // 2. Open the connection
            conn.Open();

            // 3. Pass the connection to a command object
            SqlCommand cmd = new SqlCommand("select * from Person", conn);

            //
            // 4. Use the connection
            //

            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);
            }
        }
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}






Advertisement
What's the error? Could be easily something related to user permissions and security settings.

Regards,

Vicente
Well, I'm sure this is a stupid question, but since you didn't post the error message: are you using the same DB system of the example where you took the code from? (I belive it is SQL Server).
Oops. I meant to state the error. I am actually using my own DB. I get a SqlException on my conn.open() line. It makes me think I am connecting to my database but not able to open it, I'm not sure why.

My database is an access database. You think that might be an issue?
The connection string for access is not the same as the connection string for SQL server Access connection strings Note that you will have to include the name of your mdb file in the right place in the connection string. Otherwise it won't be able to find your database.
Try this instead...

using System;using System.Data.OleDb;class OleDbTest{    public static void Main()    {        //create the database connection        OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\db1.mdb");        //create the command object and store the sql query        OleDbCommand aCommand = new OleDbCommand("select * from Person", aConnection);        try        {            aConnection.Open();            //create the datareader object to connect to table            OleDbDataReader aReader = aCommand.ExecuteReader();            Console.WriteLine("This is the returned data from emp_test table");            //Iterate throuth the database            while (aReader.Read())            {                Console.WriteLine(aReader.GetInt32(0).ToString());            }            //close the reader             aReader.Close();            //close the connection Its important.            aConnection.Close();        }        //Some usual exception handling        catch (OleDbException e)        {            Console.WriteLine("Error: {0}", e.Errors[0].Message);        }    }}


theTroll
That was it. That fixed it. Thanks Troll.

I have a question, is it a better or worse idea to do access than sql server.

What are the differences?
That was it. That fixed it. Thanks Troll.

I have a question, is it a better or worse idea to do access than sql server.

What are the differences?
It all depends on what you are trying to do.

If you want multiple connections to your database then you need to use a database server. If you are just going to access the database locally then an Access database could work just find.

There are other considerations given the size and complexity of your database but most likely those will not come into effect here.

theTroll
Is sql server more capable than access, considering all things?

Is there a gui for sql server, similar to access?

This topic is closed to new replies.

Advertisement