Meghana M

Meghana M

  • 1.6k
  • 175
  • 23.6k

how to setup connectionstring and use in console app

Mar 23 2023 9:50 AM

connectionstring configured in app.config how to use in main program console app I'm using .net 6.0 

app.config

<connectionStrings>
    <add name="Server" connectionString="Data Source=CM23-C-000SM\SQLEXPRESS;Database=Data; Trusted_Connection=true; MultipleActiveResultSets=true;TrustServerCertificate=true"/>
</connectionStrings>

program.cs

string connect = ConfigurationManager.ConnectionStrings["Server"].ConnectionString;
connect.Open();
SqlCommand command = new SqlCommand();
DataSet dataSet = new DataSet();
command.CommandText = "SpData";
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter Adapter = new SqlDataAdapter(command.CommandText, connect);
Adapter.Fill(dataSet, "SpData");
DataTable t1 = dataSet.Tables["SpData"];

foreach (DataColumn column in t1.Columns)
{
    value += column.ColumnName + '^';
}
value += "\r\n";

if (t1 != null & t1.Rows.Count > 0)
{
    foreach (DataRow row in t1.Rows)
    {       
        foreach (DataColumn column in t1.Columns)
        {
            string cellValue = row[column.ColumnName].ToString() + '^';
            value += cellValue;
        }
        value += "\r\n";
    }

 


Answers (4)