首页 新闻 会员 周边

C#新手跪求赐教

0
悬赏园豆:10 [已解决问题] 解决于 2009-12-25 10:08

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace WindowsApplication1
{
    public class Connection
    {
        SqlConnection cm;
        SqlCommand cn;
        public DataSet Set;
        public DataTable table;
        public void Creatconnection()
        {
            cm = new SqlConnection("Server=WWW-CD46602FBD8\\SQLEXPRESS;uid=sa;pwd=123;database=xk;");
        }
        public bool openconnection()
        {
            if (cm.State == ConnectionState.Closed)
                cm.Open();
            return true;
        }
        public bool closedconnection()
        {
            if (cm.State == ConnectionState.Open)
                cm.Close();
            return false;
        }
        public void Adapter(string Sqlstring)
        {
            SqlDataAdapter adapter;
            cn = new SqlCommand();
            Set = new DataSet();
            table = new DataTable();
            cn.Connection = cm;
            adapter = new SqlDataAdapter(Sqlstring, cm);
        }

public string password(string password,string table,string id)
        {
            cn = new SqlCommand();
            cn.Connection = cm;
            cn.CommandText = "select "+id+" from "+table+" where "+id+"="+password;
            cn.CommandType = CommandType.Text;
            SqlDataReader reader = cn.ExecuteReader();//There is already an open DataReader associated with this Command which must be closed first.
            if (reader.Read())
                return reader[id].ToString();
            else
                return ""; 
        }

 

private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2;
            f2=new Form2();
            Form3 f3;
            f3 = new Form3();
             Connection cd;
            cd = new Connection();
            cd.Creatconnection();
            cd.openconnection();
            if (radioButton1.Checked == true)
            {
                if (textBox1.Text == cd.password(textBox1.Text, "student", "id") && textBox2.Text == cd.password(textBox2.Text, "student", "pwd"))
                {
                    this.Hide();
                    f2.Show();
                }
                else
                    MessageBox.Show("用户名不存在或密码错误.");
            }
            else if (radioButton2.Checked == true)
            {
                if (textBox1.Text == cd.password(textBox1.Text, "course", "id") && textBox2.Text == cd.password(textBox2.Text, "course", "pwd"))
                {
                    this.Hide();
                    f3.Show();
                }
                else
                    MessageBox.Show("用户名不存在或密码错误.");
            }
            else
                MessageBox.Show("请选择职称.");
            cd.closedconnection();
        }

愿各位大侠见义勇为,帮帮忙,指点指点,祝大家平安夜快乐.

问题补充: 谢谢 贼寇在何方 烈火★寒冰的答案,问题已解决. 正确代码: public string password(string password,string table,string id) { cn = new SqlCommand(); cn.Connection = cm; cn.CommandText = "select "+id+" from "+table+" where "+id+"='"+password+"'"; cn.CommandType = CommandType.Text; this.openconnection(); SqlDataReader reader = cn.ExecuteReader();//There is already an open DataReader associated with this Command which must be closed first. if (reader.Read()) password = reader[id].ToString(); else password ="" ; this.closedconnection(); return password; }
ˊ小豬。?的主页 ˊ小豬。? | 初学一级 | 园豆:190
提问于:2009-12-24 21:39
< >
分享
最佳答案
0

请问:你的问题是什么?

cn.CommandText = "select "+id+" from "+table+" where "+id+"="+password;

改为以下试试:

cn.CommandText = "select "+id+" from "+table+" where "+id+"= ‘ "+password+ “ ' ”;

烈火★寒冰 | 菜鸟二级 |园豆:210 | 2009-12-24 21:54
其他回答(3)
0

SqlDataReader用完了得Close的

收获园豆:10
贼寇在何方 | 园豆:255 (菜鸟二级) | 2009-12-24 21:49
0

同样不清楚您的问题是什么?是红字部分?1楼和2楼都是正解,

另外您的 openconnection()  和  closedconnection()两个函数总是返回 true和false似乎没有起到什么特别作用。

我觉得应该改为:

    public void Openconnection()  //方法名首字母大写
        {
            if (cm.State == ConnectionState.Closed || cm.State == ConnectionState.Broken )
                cm.Open();
        }

十四 | 园豆:46 (初学一级) | 2009-12-25 09:05
0

什么问题啊?用using吧!

YangLau | 园豆:313 (菜鸟二级) | 2009-12-25 09:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册