Code example for SQL DB connection can't be compiled

2 posts, 1 answers
  1. algot
    algot avatar
    57 posts
    Member since:
    Nov 2011

    Posted 28 Dec 2011 Link to this post

    I try to use following code from example
    hhttp://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/general/access-sql-database.aspx
    SqlConnection thisConnection = new SqlConnection("Data Source=.\\sqlexpress; Initial Catalog=myFirstDB; Integrated Security=true;");
       
    //We define a new SQL connection with a connection string. 
    //The connection string will be different depending on your usecase and the name of the Database, table etc.
    //If you don't know how to write a connection string please look for tutorials on the Internet
         
    thisConnection.Open();
    Log.WriteLine(thisConnection.Database);
    //Wrie the name of the database to the Log
    SqlCommand thisCommand = thisConnection.CreateCommand();
    //Create an SQL command
    thisCommand.CommandText = "SELECT test FROM MyFirstTable";
    //This is a simple SQL command that will go through all the values in the "test" column from table "MyFirstTable"
    //The SQL command will vary depending on your usecase. 
    //If you don't know how to write SQL commands please look for tutorials on the Internet                   
    SqlDataReader thisReader = thisCommand.ExecuteReader();
    //Create SQL reader
    while (thisReader.Read()) {
    //Loop to go through the entire column
        //Write each value to the log
        //You can implement you own custom logic here
        Log.WriteLine("Value of test column:" + thisReader("test"));
    }
    thisReader.Close();
    //Close Reader
    thisConnection.Close();
    //Close connection to SQL database
    But compilation error occurs:
    'thisReader' is a 'variable' but is used like a 'method'
    for this statement
    Log.WriteLine("Value of test column:" + thisReader("test"));
    What I did wrong? Or is there the error?
  2. Answer
    Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 28 Dec 2011 Link to this post

    Hello Alexander,
    you're right - that was a mistake on my part. You should use these type of brackets '[' because we're using indexing (not sure if that's the correct term) to access the value. So the code should look like this:
    Log.WriteLine("Value of test column:" + (String) thisReader["test"]);

    I've fixed the code sample on the page. Thank you for your feedback no this, 100 Telerik points added to your account.

    Greetings,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top