12

我正在使用 C# 构建一个 Windows 应用程序。在我的登录表单中,我得到Select Command 属性在调用 fill 方法之前没有被初始化

这是代码:

public partial class frmlogin : Form
{
    SqlConnection con = new SqlConnection("Data Source=TH07L019;Initial Catalog=loginerror;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp = new SqlDataAdapter();

    public frmlogin()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cmd.Connection = con;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd.CommandText = "select * from login where username='" + txt1.Text + "' and password='" + txt2.Text +"'";
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            frmmain main = new frmmain();
            main.Show();
        }
        else
        {
            MessageBox.Show("Please enter correct name and passowrd", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
4

4 回答 4

19

在填充表之前,您必须指定 SqlDataAdapter 的选择命令。你没有这样做。您的 SqlCommand 对象未以任何方式连接到您的 SqlDataAdapter。

 adp.SelectCommand=cmd;
于 2013-04-04T06:24:02.047 回答
7

另一种实现方法是将 SQLCommand 作为参数简单地传递到您的数据适配器中,如下所示 -

SqlCommand cmd = new SqlCommand();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
于 2016-06-25T22:23:06.493 回答
0

SQL 数据适配器与数据表交互。它用于从 SQL Server 数据库中填充数据表。在填充数据表之前,数据适配器应该知道它将要执行的命令。所以我们必须填充 SQL 命令类型的对象,即

SqlDataAdapter da = new SqlDataAdapter(cmd);

cmd是 SQL 命令的对象。

现在,数据适配器将知道在填充数据表之前要执行哪个命令。

于 2019-04-25T07:39:00.817 回答
0
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 Employees_Details_Management_System
{
    public partial class ShowEmpDtlsFrm : Form
    {

        SqlConnection con = new SqlConnection(@"Data Source = (local); Initial Catalog = sp_emp; Integrated Security = True");

        public SqlCommand cmd { get; private set; }

        // private SqlCommand cmd;

        public ShowEmpDtlsFrm()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ShwEmpDtlsLbl.Text = "Employee Database Management";
            comboBox1.Text = "Click";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public void DataGridChk()//Check if DataGrid is empty or not
        {
           if (dataGridView1.RowCount == 1)
            {
                dataGridView1.Visible = false;
                MessageBox.Show("The Given " + comboBox1.Text+ " is Invalid  \nEnter Valid " + comboBox1.Text);
            }
        }

        public void DbDataFetch(String Qry)//For Fetching data from database
        {
            SqlCommand cmd = new SqlCommand(Qry, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "emp");
            dataGridView1.DataSource = ds.Tables["emp"].DefaultView;
            DataGridChk();
        }

        public void SrchBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.Text == "Employee ID" || comboBox1.Text == "Department ID" || comboBox1.Text == "Manager ID")
                {
                    if (textBox1.Text != "")
                    {
                        con.Open();
                        dataGridView1.Visible = true;
                        if (comboBox1.Text == "Employee ID")
                        {
                            string Qry = "select * from emp where emp_id='" + textBox1.Text + "' ";
                            DbDataFetch(Qry);
                        }
                        else if (comboBox1.Text == "Department ID")
                        {
                            string Qry = "select * from emp where  dep_id='" + textBox1.Text + "'  ";
                            DbDataFetch(Qry);
                        }
                        else if (comboBox1.Text == "Manager ID")
                        {
                            string Qry = "select * from emp where manager_id='" + textBox1.Text + "' ";
                            DbDataFetch(Qry);
                        }
                        con.Close();
                    }
                    else
                    {
                        MessageBox.Show("Please Enter the ID...");
                    }
                }
                else
                {
                    MessageBox.Show("Choose Valid Option...");
                }
            }
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                System.Windows.Forms.MessageBox.Show(sqlException.Message);
                con.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EmpDtlsFrm edf = new EmpDtlsFrm();
            edf.Show();
            this.Hide();
        }
    }
}
于 2019-09-20T11:18:56.217 回答