首页 新闻 会员 周边

用存储验证用户名和密码

0
悬赏园豆:10 [已关闭问题]

请大家帮我看下下面代码,我是新手,请大家帮忙。谢谢

public SqlConnection con = null;
        public SqlCommand com = null;
        public static string M_admname;
        public static string M_password;

private void button1_Click(object sender, EventArgs e)
        {
            con = gc.GetCon();
            com = new SqlCommand("proc_admin", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add("@admname", SqlDbType.VarChar, 20).Value = M_admname;
            com.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = M_password;
            con.Open();
            int p_result = com.ExecuteNonQuery();
            con.Close();
       if (p_result == 100)
            {
                M_admname = this.txtname.Text;
                M_password = this.txtpassword.Text;
                Form1 frmmain = new Form1();
                frmmain.Show();
                this.Close();
                con.Close();
          
            }
            if(p_result==-100)
            {
                MessageBox.Show("用户名或密码不正确", "友情报提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                con.Close();
                    txtname.Text="";
                txtname.Focus();
            }

        }

错误说有对象未关闭。我找了很长间都找不出来,这是个验证用户和密码的存储,

大菜鸟的主页 大菜鸟 | 初学一级 | 园豆:90
提问于:2009-04-04 14:14
< >
分享
其他回答(3)
0

第一个IF外面已经con.Close();了,在if里面两个分支里还全都要con.Close();,这样用肯定会报错吧。去掉外面的或去掉里面的。

风海迷沙 | 园豆:4453 (老鸟四级) | 2009-04-04 14:42
0

用了两次con.close();你要注意con的打开和关闭。

timzheng | 园豆:195 (初学一级) | 2009-04-04 14:42
0

两个if中的con.close()都去掉

Jaryleely | 园豆:367 (菜鸟二级) | 2009-04-04 17:19
0

if (Con.State == ConnectionState.Open)
{
  Con.Close();
}

最好用 Try...Catch...Finally 
把 Con.Close(); 放在 Finally block 里

try
{
 
// your code
}
catch (Exception ex)
{
 Response.Write(ex.Message);
}
finally
{
 
if (Con.State == ConnectionState.Open)
 {
  Con.Close();
 }
}
                        
WizardWu | 园豆:1402 (小虾三级) | 2009-04-04 17:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册