开发者

C# Access DB Queries [closed]

开发者 https://www.devze.com 2023-01-09 20:02 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. 开发者_高级运维
Closed. This question needs to be more focused. It is not currently accepting answers.
开发者_高级运维

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 6 years ago.

Improve this question

How can I perform queries on access using the C#? I want to create tables, and Insert/Select data from my access database.


You should check out all things you can do with OdbcConnection and OdbcCommand.

You can even steal the Connection String for your connection from:

Access 2007 Connection String Samples

...that should be enough to get you started.


Here's a tutorial to get you started.

http://www.csharphelp.com/2006/01/ms-access-application-with-c/

Depending on your version of Access, you may want to check out differenc connection strings as well.

http://connectionstrings.com


Here are 2 pretty good starting tutorials

Here is a good intro into what is actually going on.
Here has some pretty helpful example code.

Protip: Make sure you have the correct ODBC Drivers installed if they are not already. I felt SOOOO stupid for not figuring that out from the start lol ;p

As far as dealing with you db assuming your not creating a access db on the fly all you would have to do is create your db in access, save it, and add it as a data source to your application.See here

Example Insert:

var insertStatement = @"insert into familytree (firstname, lastname, city, Tel, Email) values (@firstname, @lastname, @city, @tel, @email); SELECT @@IDENTITY";

//Open your connection and command
using (OleDbConnection connection = new OleDbConnection(connectionString))
using (OleDbCommand cmd = new OleDbCommand(insertStatement, connection))
{
    //set parameters and values
    var identityQuery  = @"SELECT @@IDENTITY";
    var identity = -1;
    cmd.Parameters.Add("@firstname", 'foo');
    cmd.Parameters.Add("@lastname", 'foo');
    cmd.Parameters.Add("@city", 'foo');
    cmd.Parameters.Add("@tel", '6666666');
    cmd.Parameters.Add("@email", 'foo@foo.com');

    connection.Open();

    try{
        var numberOfRowsEffected = command.ExecuteNonQuery();
        //we should have 1 row effected. 
        if(numberOfRowsEffected>0){
            cmd.CommandText =  identityQuery;
            //get the identity 
            identity = (int)cmd.ExecuteScalar(); 
        }
    }catch(InvalidOperationException ex){
        //log and throw: 
        //cant open connection or Cannot execute a command 
        //within a transaction context that differs from the 
        //context in which the connection was originally enliste
    }

    return identity;
}

Same thing applies if you were wanting to create a table. just write your create table statement. see here for example and execute. But as far as common approaches go you generally want to have you table structures already set up for most simple apps and let your Application handle inserts, updates, and possibly deletes. Not saying you shouldn't do it that way but I would consider KISS whenever possible.

Oh and here is an msdn ref to the OleDbCommand class if you were wondering else you can do.
OleDbCommand

0

精彩评论

暂无评论...
验证码 换一张
取 消