mycon.myconn是数据库的名字么?
if(Page.IsValid )
{
SqlConnection1 = new SqlConnection(ConfigurationSettings.AppSettings["dsn"]);
SqlCommand1=new SqlCommand("sp_addAdminValidate",SqlConnection1);
SqlCommand1.CommandType=CommandType.StoredProcedure;
SqlCommand1.Parameters.Add("@username",SqlDbType.Char,40);
SqlCommand1.Parameters["@username"].Value =Username1.Text.Trim();
try
{
SqlConnection1.Open();
SqlDataReader1=SqlCommand1.ExecuteReader();
if(SqlDataReader1.Read()==true)
{
myLabel.Text="已有此管理员,请重新输入姓名!!" ;
SqlDataReader1.Close();
}
else
{
SqlDataReader1.Close();
SqlCommand1=new SqlCommand("sp_addAdmin",SqlConnection1);
SqlCommand1.CommandType=CommandType.StoredProcedure;
SqlCommand1.Parameters.Add("@username", SqlDbType.Char,40);
SqlCommand1.Parameters["@username"].Value =Username1.Text.Trim();
SqlCommand1.Parameters.Add("@password",SqlDbType.Char,40);
SqlCommand1.Parameters["@password"].Value = Password1.Text.Trim();
SqlCommand1.Parameters.Add("@userclass",SqlDbType.Char,20);
SqlCommand1.Parameters["@userclass"].Value = UserClass1.SelectedItem.Text.Trim();
SqlCommand1.Parameters.Add("@remark",SqlDbType.NChar,200);
SqlCommand1.Parameters["@remark"].Value = Remarks.Text.Trim();
try
{
int i=SqlCommand1.ExecuteNonQuery();
if(i>0)
{
myLabel.Text="添加管理员成功!";
}
}
catch (SqlException e1)
{
myLabel.Text ="数据库操作错误:"+e1.Message;
}
}
}
catch (SqlException e1)
{
myLabel.Text ="数据库操作错误:"+e1.Message;
}
finally
{
SqlDataReader1.Close();
SqlConnection1.Close();
}
}
}
}
其中的【dns】为数据库链接在web.config中
sp_addAdminValidate为存储过程ALTER PROCEDURE sp_addAdminValidate @username char(100)AS select * from db_Admin where username=@username
Username1.Text为输入的用户名