App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="main" providerName="System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=mydatabase;User Id=myid;Password=mypassword;"/>
</connectionStrings>
<configuration>
Код C #:
using System.Configuration;
public string _connectionString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
public string GetString(string sql, SqlParameter[] parameters)
{
List<string> result = new List<string>();
SqlDataReader reader;
using (SqlConnection connection = new SqlConnection(_connectionString ))
{
SqlCommand command = new SqlCommand(sql, connection);
if (parameters != null)
{
command.Parameters.Clear();
command.Parameters.AddRange(parameters);
}
connection.Open();
reader = command.ExecuteReader();
while (reader.Read())
{
string row = string.Empty;
foreach (string col in reader)
{
row += col + ",";
}
result.Add(row)
}
reader.Close();
}
return result;
}