This is a migrated thread and some comments may be shown as answers.

Code example for SQL DB connection can't be compiled

1 Answer 104 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
algot
Top achievements
Rank 1
algot asked on 28 Dec 2011, 12:26 PM
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?

1 Answer, 1 is accepted

Sort by
0
Accepted
Stoich
Telerik team
answered on 28 Dec 2011, 01:49 PM
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
Tags
General Discussions
Asked by
algot
Top achievements
Rank 1
Answers by
Stoich
Telerik team
Share this question
or