c# web.config app.config

  • 139
  • 0
  • 2019-07-29

g

<appSettings>
    <add key="conStr" value="Data Source=資料庫位置;Initial Catalog=資料庫名稱;User ID=帳號;Password=密碼"/>
  </appSettings>

string connectStr;
string sql;
SqlConnection sqlCon;
SqlDataAdapter sqlAdp;
SqlCommand cmd;
DataSet ds;
DataView dv;

//透過ConfigurationManager來抓取我們剛剛在Web.config中設定的連線字串
connectStr=ConfigurationManager.AppSettings["conStr"];

----------------------------------------------------
var x = "Data Source=192.168.100.3;Initial Catalog=CCYS;User ID=sa;Password=I5m4p1e2l5e8x64";
using (SqlConnection sqlCon = new SqlConnection(x))
{
   using (SqlDataAdapter sqlAdp = new SqlDataAdapter())
   {
      SqlCommand cmd = new SqlCommand("select * from Fwoth", sqlCon);
      sqlAdp.SelectCommand = cmd;

      DataSet ds = new DataSet();
      sqlAdp.Fill(ds);
      var c = ds.Tables[0];
   }

}
using (StreamWriter sw = new StreamWriter("TestFile.TXT"))   //小寫TXT     
{
    // Add some text to the file.
    sw.Write("This is the ");
    sw.WriteLine("header for the file.");
    sw.WriteLine("-------------------");
    // Arbitrary objects can also be written to the file.
    sw.Write("The date is: ");
    sw.WriteLine(DateTime.Now);
}

using (StreamReader sr = new StreamReader("TestFile.TXT"))     //小寫TXT
{
    String line;
    // Read and display lines from the file until the end of 
    // the file is reached.
    while ((line = sr.ReadLine()) != null) 
    {
        Console.WriteLine(line);
    }
}