Sign in to follow this  
ELFanatic

[.net] C# connection to a db

Recommended Posts

ELFanatic    100
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();
            }
        }
    }
}






Share this post


Link to post
Share on other sites
cignox1    735
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).

Share this post


Link to post
Share on other sites
ELFanatic    100
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?

Share this post


Link to post
Share on other sites
turnpast    1011
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.

Share this post


Link to post
Share on other sites
TheTroll    883
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

Share this post


Link to post
Share on other sites
TheTroll    883
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

Share this post


Link to post
Share on other sites
SnOrfys    200
Quote:
Original post by deathwearer
Yes there is a tool for SQL server called : Management studio (Which is a pain to use sometime because of some GUI limits but it still do the job)


I'll second that... the db server is good, but the UI for Management Studio Express is pretty flaky.

Share this post


Link to post
Share on other sites
daviangel    604
Quote:
Original post by ELFanatic
Is sql server more capable than access, considering all things?

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

You can download sql server 2005 express and management studio express and check it out for yourself since it's all free.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this