猿问

C#新手跪求赐教

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();
        }

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

万千封印
浏览 497回答 4
4回答

子衿沉夜

请问:你的问题是什么? cn.CommandText = "select "+id+" from "+table+" where "+id+"="+password; 改为以下试试: cn.CommandText = "select "+id+" from "+table+" where "+id+"= ‘ "+password+ “ ' ”;

阿波罗的战车

SqlDataReader用完了得Close的

慕森卡

同样不清楚您的问题是什么?是红字部分?1楼和2楼都是正解, 另外您的 openconnection()  和  closedconnection()两个函数总是返回 true和false似乎没有起到什么特别作用。 我觉得应该改为:     public void Openconnection()  //方法名首字母大写        {            if (cm.State == ConnectionState.Closed || cm.State == ConnectionState.Broken )                cm.Open();        }

Helenr

什么问题啊?用using吧!
随时随地看视频慕课网APP
我要回答