C# MSSQL 連線範例 (粗糙版)

C# MSSQL

Web.config
	<configuration>
	  <connectionStrings>
		<add name="testDb" connectionString="data source=TESTAABBCC;initial catalog=MsSqlDataBase;user id=super;password=Abc123456;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
	  </connectionStrings> 
	</configuration>
TestClass.cs
	string id = "123";
	string mail = "tret@mail.com";
	string _ConnectionString = WebConfigurationManager.ConnectionStrings["testDb"].ConnectionString.ToString();
	string queryString = @" SELECT * 
				FROM dbo.testDb01 WITH (NOLOCK) 
				WHERE 1 = 1 
				AMD id = @id 
				AMD mail = @mail ";

	DataTable dt;
	DatabaseConnection db = new DatabaseConnection();           //建立資料庫物件
	using (SqlConnection conn = new SqlConnection(_ConnectionString))
	{
		SqlCommand cmd = new SqlCommand(queryString, conn);
		cmd.CommandType = CommandType.Text;
		cmd.Parameters.Clear();
		cmd.Parameters.Add(new SqlParameter("@id", id));         //參數
		cmd.Parameters.Add(new SqlParameter("@mail", mail));     //參數
		conn.Open();//連線         
		SqlDataReader dr = cmd.ExecuteReader();
		dt = new DataTable();
		dt.Load(dr);
		dr.Close();
		conn.Close();
	}

 

我只是一棵樹