-5

My connection string:

string connection = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
    @"Data Source=\\reso-fs-2\allusers\Student_Home\20350657\Documents\clicker.accdb;" +
    @"Jet OLEDB:Database Password=" + "password" + ";";

OleDbConnection con = new OleDbConnection(connection);

So I have this query in C# OleDb:

string query = "SELECT stats_best FROM Users WHERE username="+GameForm.username;

I want to fetch the value from 'stats_best' and save it into a string. I have already set-up the connection and all that. I just need to return a value from the query. How can I do that?

Pawel
  • 29
  • 1
  • 5

1 Answers1

0

Please Read this article, but anyway, you can use this

public string Test(string userName, string connectionString, out string dbErrorMessage)
    {
        string result = null;
        dbErrorMessage = null;
        try
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                SqlCommand cmd = connection.CreateCommand();
                cmd.Parameters.Add(new SqlParameter("@UserName", userName));
                cmd.CommandText = "SELECT stats_best FROM Users WHERE username= @UserName";
                result = cmd.ExecuteScalar().ToString();
            }
        }
        catch (Exception ex)
        {
            dbErrorMessage = ex.Message;
        }
        return result;
    }

and the usage of method:

string dbErrorMessage = null;
Test(GameForm.username, connectionString, out dbErrorMessage);
SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • This seems to execute but it doesn't return the value from the database. – Pawel Mar 15 '16 at 10:27
  • did you check your `ConnectionString`? – SᴇM Mar 15 '16 at 10:28
  • I can't check your connection string from here, please check if it works. And what is result of execution? – SᴇM Mar 15 '16 at 10:31
  • The program executes but doesn't return a value. Maybe it's because i'm using OleDbConnection rather than SqlConnection. Could this method be adapted to OleDbConnection? – Pawel Mar 15 '16 at 10:33
  • `"Data Source=\\reso-fs-2\allusers\Student_Home\20350657\Documents\clicker.accdb;Initial Catalog=;User=;Password=;"`, something like this – SᴇM Mar 15 '16 at 10:40
  • Nothing seems to be returning the string. Thank you for your help though. – Pawel Mar 15 '16 at 10:50