马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#未将对象引用设置到对象的实例求助!!新手求帮助

[复制链接]
查看3801 | 回复3 | 2014-12-23 13:24:04 | 显示全部楼层 |阅读模式
VS2013 SQL2012做的。求助 。。到底是什么问题
ttaly22 | 2014-12-23 13:25:18 | 显示全部楼层
help
1.jpg
2.png
3.jpg
ttaly22 | 2014-12-23 13:28:36 | 显示全部楼层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//数据库连接
using System.Data;//connectionstate使用
namespace WindowsFormsApplication2
{
    class Dbhelp
    {   ///创建数据库连接字符串///
         string connStr = "Data Source=.;Initial Catalog=school;Integrated Security=True";
        ///数据库连接对象///
          SqlConnection conn;
        /// <summary>
        /// 数据库连接对象
        /// </summary>
        public SqlConnection Conn
        {
            get
            {
                 if(conn==null)
                 {
                     conn = new SqlConnection(connStr);              
                }
                return conn;
            }
            set { conn = value; }
        }
         /// <summary>
         /// 打开数据库连接
         /// </summary>
        public void openconnection()
        {
            if(conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            else if(conn.State == ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }
        }
            public void closeconnection()
            {
                if(conn.State==ConnectionState.Open||conn.State==ConnectionState.Broken)
                {
                    conn.Close();
                }
            }
        }
    }



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

namespace WindowsFormsApplication2
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.cboid.SelectedIndex = 0;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (InputCheck())
            {
                if (login())
                {
                    if (this.cboid.Text == "学生")
                    {
                        Frmstu frmstu = new Frmstu();
                        frmstu.Show();
                        this.Hide();
                        MessageBox.Show("登陆成功");
                    }

                    else if (this.cboid.Text == "管理员")
                    {
                        Admin admin = new Admin();
                        admin.Show();
                        this.Hide();
                        MessageBox.Show("登陆成功");
                    }
                  
                }
               
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {///关闭///
            this.Close();
        }
        private void txtpassword_TextChanged(object sender, EventArgs e)
        {
        }
        private void txtusername_TextChanged(object sender, EventArgs e)
        {
        }
        private void cboid_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        public bool InputCheck()
        {
            bool flag = true;
            if (string.IsNullOrEmpty(this.txtusername.Text.Trim()))
            {
                MessageBox.Show("请输入用户名");
                this.txtusername.Focus();
                flag = false;
            }
            else if (string.IsNullOrEmpty(this.txtpassword.Text.Trim()))
            {
                MessageBox.Show("请输入密码");
                this.txtpassword.Focus();
                flag = false;
            }
            else if (this.cboid.Text == "请选择")
            {
                MessageBox.Show("请选择登陆类型");
                flag = false;
            }
            return flag;
        }
        public bool login()
        {
                bool result = false;
                Dbhelp db = new Dbhelp();
             try
            {   ///打开数据库///
                db.openconnection();
                //创建SQL语句
                string sql = string.Format("Select count(*) from login where username='{0}' and password={1}",this.txtusername.Text.Trim(),this.txtpassword.Text.Trim());
                //命令对象
                SqlCommand comm = new SqlCommand(sql,db.Conn);
                //执行并返回判断
                int num = (int)comm.ExecuteScalar();
                if (num > 0)
                {
                    result = true;
                }
                else MessageBox.Show("用户名或者密码不正确");
            }
            catch (Exception ex)
            {
               
                MessageBox.Show("error"+ex.Message);
            }
            finally
             {
                 db.closeconnection();
             }
             return result;
            
        }
    }
}
真心求助。。。着急!

ibcadmin | 2014-12-23 15:57:20 | 显示全部楼层
检查db有没有new  有没有实例化
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则