ASP.NET連線SQL SERVER
阿新 • • 發佈:2018-12-15
Web.config
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.;Initial Catalog=asp-test;User ID=;Password=;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
查詢測試
string sqlconnstr=ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection sqlconn = new SqlConnection(sqlconnstr); SqlCommand sqlcommand = new SqlCommand(); sqlcommand.Connection = sqlconn; sqlconn.Open(); sqlcommand.CommandText = "select * from uesr"; SqlDataReader sqldatareader = sqlcommand.ExecuteReader(); while (sqldatareader.Read()) { Label1.Text += sqldatareader.GetInt32(0)+"<span style='color:Red'>||||||</span>"; Label1.Text += sqldatareader.GetString(1) + "<span style='color:Red'>||||||</span>"; Label1.Text += sqldatareader.GetInt32(2) + "<span style='color:Red'>||||||</span>"; Label1.Text += sqldatareader.GetInt32(3); }; sqlcommand = null; sqlconn.Close(); sqlconn = null;