为了账号安全,请及时绑定邮箱和手机立即绑定

C#新手跪求赐教

C#新手跪求赐教

万千封印 2018-12-06 19:43:09
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();        } 愿各位大侠见义勇为,帮帮忙,指点指点,祝大家平安夜快乐.
查看完整描述

4 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

请问:你的问题是什么?

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

改为以下试试:

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

查看完整回答
反对 回复 2019-01-21
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

SqlDataReader用完了得Close的

查看完整回答
反对 回复 2019-01-21
?
慕森卡

TA贡献1806条经验 获得超8个赞

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

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

我觉得应该改为:

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

查看完整回答
反对 回复 2019-01-21
?
Helenr

TA贡献1780条经验 获得超4个赞

什么问题啊?用using吧!

查看完整回答
反对 回复 2019-01-21
  • 4 回答
  • 0 关注
  • 496 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信