Web.config页面
<connectionStrings>
<add name ="sqlserverstr" connectionString="Data Source=电脑名;Initial Catalog=数据库名;User ID=数据库用户名;Password=数据库密码"/>
</connectionStrings>
后台aspx.cs页面
例:#region 返回第一行,第一列,判断用户名是否存在
bool result = true;
string sqlstr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
SqlConnection con = new SqlConnection(sqlstr);
SqlCommand cmd = con.CreateCommand();
con.Open();
= "select count(*) from T_User where UserName=@username";
cmd.Parameters.AddWithValue("@username", txbUserName.Text.Trim());
int count = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Dispose();
con.Close();
con.Dispose();
if (count > 0)
{
result = false;
}
return result;
#endregion