我用的是login组件,登陆按钮触发的事件是自己写的:
protected void LoginButton_Click(object sender, EventArgs e)
{
int num = 0;//返回受影响行数
//连接数据库的字符串
string connstr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["OneTreeDBConnectionString"].ToString();
SqlConnection myconn = new SqlConnection(connstr);
//建立记录字符串
string username = User_Login.UserName.Trim().ToLower();
string password = User_Login.Password.Trim().ToLower();
// string mysql = "select * from admin_table where [user_name]='" + username+"'";
string mysql = string.Format("select * from [admin_table] where [user_name]='{0}' and [user_pwd]='{1}'", username, password);
SqlCommand mycmd=new SqlCommand(mysql,myconn);
try
{
myconn.Open();//打开数据库
num = Convert.ToInt32(mycmd.ExecuteNonQuery());
}
catch (Exception ex)
{
Response.Write("系统数据操作错误:" + ex.ToString());
}
if (num<1)
User_Login.FailureText = "用户名密码错误!";
else
Response.Redirect("admin.aspx"); myconn.Close();
}调试出错:
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
protected void LoginButton_Click(object sender, EventArgs e)
{
int num = 0;//返回受影响行数
//连接数据库的字符串
string connstr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["OneTreeDBConnectionString"].ToString();
SqlConnection myconn = new SqlConnection(connstr);
//建立记录字符串
string username = User_Login.UserName.Trim().ToLower();
string password = User_Login.Password.Trim().ToLower();
// string mysql = "select * from admin_table where [user_name]='" + username+"'";
string mysql = string.Format("select * from [admin_table] where [user_name]='{0}' and [user_pwd]='{1}'", username, password);
SqlCommand mycmd=new SqlCommand(mysql,myconn);
try
{
myconn.Open();//打开数据库
num = Convert.ToInt32(mycmd.ExecuteNonQuery());
}
catch (Exception ex)
{
Response.Write("系统数据操作错误:" + ex.ToString());
}
if (num<1)
User_Login.FailureText = "用户名密码错误!";
else
Response.Redirect("admin.aspx"); myconn.Close();
}调试出错:
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
myconn.open();
{
myconn.Open();//打开数据库
num = Convert.ToInt32(mycmd.ExecuteNonQuery());
}
怎么能放在SqlCommand后面呢?当然先得打开connection了啊
//连接数据库的字符串
string connstr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["OneTreeDBConnectionString"].ToString();
SqlConnection myconn = new SqlConnection(connstr);
myconn.Open();//打开数据库
//建立记录字符串
string username = User_Login.UserName.Trim().ToLower();
string password = User_Login.Password.Trim().ToLower();
// string mysql = "select * from admin_table where [user_name]='" + username+"'";
string mysql = string.Format("select * from [admin_table] where [user_name]='{0}' and [user_pwd]='{1}'", username, password);
SqlCommand mycmd=new SqlCommand(mysql,myconn);
try
{
num = Convert.ToInt32(mycmd.ExecuteNonQuery());
}
还是不行啊:
在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)