错误处理:连接属性尚未初始化c# [英] Error Handling : Connection Property Has Not Been Initialized c#

查看:82
本文介绍了错误处理:连接属性尚未初始化c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这方面的帮助而且我没有站立如何解决此Executereader:连接属性尚未初始化。



I Need Help For This Below And I Not Under Stand How To Solve This Executereader: Connection Property Has Not Been Initialized.

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

namespace WindowsFormsApplication1
{
    public partial class frmlgn : Form
    {
        OleDbCommand cmd;
        
        public frmlgn()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            cmd = new OleDbCommand("select * from login where id='" + textBox1.Text + "'", Class1.con);
            OleDbDataReader dr;
            dr = cmd.ExecuteReader();
            dr.Read();
            if (dr.HasRows == true)
            {
                if (dr["password"].ToString() != textBox2.Text)
                {
                    MessageBox.Show("Password Not Matched!");
                    textBox2.Focus();
                }
                else
                {
                    MessageBox.Show("successfuly login");
                    this.Close();
                    MDIParent2 mdi=new MDIParent2();
                    
                                                       

                }
                }
            else
                MessageBox.Show("Invalid User id");
            dr.Close();
        }









我用过这个连接字符串class











I used this connection string in class



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    class Class1
    {
        public static OleDbConnection con;
        public static void Connection()
        {

           con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= e:\db1.mdb");
            con.Open();
        }
    }
}

推荐答案

先生,请检查上面的代码,你有做了连接功能,但你没有在
sir, please check as in the code above you have made the Connection function but you haven't used it before
cmd = new OleDbCommand("select * from login where id='" + textBox1.Text + "'", Class1.con);


您的代码存在许多问题。主要的一个是你永远不会调用Class1.Connection(),这就是Class1.con为空的原因。


0)Class1不仅执行得不好,而且没有提供你的代码任何东西,所以你应该摆脱它。

1)< SHOUTING>不要存储普通文本密码! < / SHOUTING>

2)密码的测试应该在数据库端进行。

3)不要使用连接来形成查询;使用参数化查询 - 这很容易。

4)不要测试HasRows;它是不必要的,并非所有提供商都实现它 - 只测试Read的结果。

5)在你需要使用它之前不要打开连接。

6)使用using语句。
There are a number of problems with your code. The main one is you never call Class1.Connection() , that's why Class1.con is null.

0) Class1 is not only poorly implemented, but doesn't offer your code anything, so you should get rid of it.
1) <SHOUTING> DO NOT STORE PLAIN-TEXT PASSWORDS !!! </SHOUTING>
2) The test of the password should occur on the database side.
3) Do not use concatenation to form the query; use a parameterized query -- it's very easy.
4) Don't test HasRows; it's needless and not all providers implement it -- just test the result of Read.
5) Don't Open the Connection until you need to use it.
6) Use Using statements.


您好,感谢您在代码中解决问题的所有帮助已被修改,现在已成功运行



如下所示





Hello and thank you for all the help in solving the problem in the code has been modified and is now running successfully

As shown below


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

namespace WindowsFormsApplication1
{
    public partial class frmlgn : Form
    {
       OleDbCommand cmd = new OleDbCommand();
        OleDbConnection con = new OleDbConnection();
        OleDbDataReader dr;
  public frmlgn()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.ConnectionString = Class1.con;
                con.Open();
                cmd.Parameters.Clear();
                cmd.Connection = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "select * from login where id='" + textBox1.Text + "'";
                OleDbDataReader dr;
                dr = cmd.ExecuteReader();
                dr.Read();
                if (dr.HasRows == true)
                {
                    if (dr["password"].ToString() != textBox2.Text)
                    {
                        MessageBox.Show("Password Not Matched!");
                        textBox2.Focus();
                    }
                    else
                    {
                        MessageBox.Show("successfuly login");

                       
                        
                        Form1 a = new Form1();
                        this.Hide();
                        a.Show();
                        


                    }
                }
                else
                    MessageBox.Show("Invalid User id");
                dr.Close();
            }


and connection string   Class1 

As shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
    class Class1
{

        internal static string con = (@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= d:\db1.mdb");
      
        }
    }


这篇关于错误处理:连接属性尚未初始化c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆