Hi, I have a problem fully understanding properties. Below is the code and my question will follow righ after it
Here is my problem. Basically, I want to be able to set connection string and select(sql statement) string through the Main( ), fill the data set and display the records in the console window. What I would like to do next, is to pass (set) a new connection string and select string to the ConnectionClass and display those records; however, _ConnectionString and _SelectString remember the old values and display the records from the first step. When I stepped through the code I noticed that I was able to set new values to the two properties, however, as soon as I came back to my program class to call “connect.ConnectToDB("Authors");” the values in my properies change back to the old ones. How do I overcome this problem?
Thanks!
Valeriya
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace SelectIntoDataSet
{
class Program
{
static void Main(string[] args)
{
string connectToDb = "server= ENGSQL1;database=Northwind;Integrated Security=True";
string selectStr = "Select Top 10 CustomerID, CompanyName, ContactName, Address " +
"From Customers Order by CustomerID";
ConnectionClass connect = new ConnectionClass(); //(connectToDb,selectStr);
connect._ConnectionString = connectToDb;
connect._SelectString = selectStr;
connect.ConnectToDB("Customers");
string connectToDb1 = "server= EngSQL1;database = pubs;Integrated Security=True";
string selectStr1 = "Select * From authors";
ConnectionClass connect1 = new ConnectionClass(); //(connectToDb1, selectStr1);
connect1._ConnectionString = connectToDb1;
connect1._SelectString = selectStr1;
connect.ConnectToDB("Authors");
Console.Read();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SelectIntoDataSet
{
public class ConnectionClass
{
private string connectionString;
private string selectString;
public string _ConnectionString
{
get{return connectionString;}
set{connectionString = value;}
}
public string _SelectString
{
get{return selectString;}
set {selectString = value;}
}
public void ConnectToDB(string tableName)
{
SqlConnection mySqlConnection = new SqlConnection(_ConnectionString);
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = _SelectString;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
mySqlDataAdapter.Fill(myDataSet, tableName);
mySqlConnection.Close();
DataTable myDataTable = myDataSet.Tables[tableName];
DisplayRows(myDataTable);
}
public void DisplayRows(DataTable table)
{
foreach (DataRow myDataRow in table.Rows)
{
for(int j = 0; j < table.Columns.Count; j++)
{
Console.WriteLine(table.Columns[j] + " = " + myDataRow[ table.Columns[j] ]);
}
}
Console.WriteLine(table.Rows.Count);
Console.Read();
}
}
}
Here is my problem. Basically, I want to be able to set connection string and select(sql statement) string through the Main( ), fill the data set and display the records in the console window. What I would like to do next, is to pass (set) a new connection string and select string to the ConnectionClass and display those records; however, _ConnectionString and _SelectString remember the old values and display the records from the first step. When I stepped through the code I noticed that I was able to set new values to the two properties, however, as soon as I came back to my program class to call “connect.ConnectToDB("Authors");” the values in my properies change back to the old ones. How do I overcome this problem?
Thanks!
Valeriya