Jump to content
  • Advertisement
Sign in to follow this  
ELFanatic

[.net] C# connection to a db

This topic is 3890 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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?

Share this post


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

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!