问题 为什么我没有足够的参数提供给福彩12选5走势图错误

罗兰斯富贸易

活跃的成员
加入
2019年2月3日
消息
42
地点
布里奇德
编程经验
Beginner
我试图读取数据库并将其显示为DataGridView,但我一直收到错误
C#:
private void nameSearch()
{
    using (SQLiteCommand cmd = connAccount.CreateCommand())
    {
        connAccount.Open();
        try
        {
            connAccount = new SQLiteConnection(dbConnection.source);

            cmd.CommandText = @"SELECT accid, account.custid, customer.title, customer.firstname || ' ' || customer.lastname AS Name, product.isaname AS ProductName, balance, accrued, active FROM account INNER JOIN customer ON customer.custid = account.custid INNER JOIN product ON product.prodid = account.prodid WHERE Name = @setName";

            cmd.Parameters.AddWithValue("@setName", txt_name.Text);
            cmd.ExecuteNonQuery();

            da_Accounts = new SQLiteDataAdapter(cmd.CommandText, connAccount);
            dt_Accounts = new DataTable();
            da_Accounts.Fill(dt_Accounts);
            dgv_Account.DataSource = dt_Accounts;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
 
最后编辑了主持人:

jmplhinney.

C#论坛主持人
工作人员
加入
2011年4月23日
消息
3,692
地点
悉尼,澳大利亚
编程经验
10+
你真的应该花更多的时间学习Ado.net的基础知识,因为那里有很多错误。像这样的代码,当人们不考虑在写作之前实际要做什么时会发生什么。我不是在谈论最终结果,而是到达那里的步骤。如果您想到逻辑,并完成需要执行的步骤,那么编写代码以具体地实现这些步骤,您将不会结束代码做出无意义的东西或不执行所需的东西。让我们来看看那个代码,并考虑它所实际的东西。
C#:
using (SQLiteCommand cmd = connAccount.CreateCommand())
该行创建了一个新的福彩12选5走势图对象。好的。
C#:
cmd.CommandText = @"SELECT accid, account.custid, customer.title, customer.firstname || ' ' || customer.lastname AS Name, product.isaname AS ProductName, balance, accrued, active FROM account INNER JOIN customer ON customer.custid = account.custid INNER JOIN product ON product.prodid = account.prodid WHERE Name = @setName";
That line sets the SQL code for that command to a SELECT statement, i.e. a query. OK
C#:
cmd.Parameters.AddWithValue("@setName", txt_name.Text);
That line adds a parameter to the command. Why you would be using "@setName" instead of just "@Name" when the parameter is specifically to match the Name column is beyond me, but bad names will still work so we're still OK.
C#:
cmd.ExecuteNonQuery();
Now you have gone off the rails. This is what I mean about code doing pointless things. What exactly is that line supposed to be achieving? If you wrote an algorithm, what step would that be performing? The ExecuteNonQuery method is specifically for executing SQL statements that do not produce a result set, i.e. non-queries. What does a SELECT statement do? It produces a result set, i.e. it's a query. You obviously want the contents of that result set so what is the reason that you are calling ExecuteNonQuery? There isn;t one. You have code for no reason.
C#:
da_Accounts = new SQLiteDataAdapter(cmd.CommandText, connAccount);
在这里,您可以进一步了解轨道。您使用与使用相同的SQL查询创建数据适配器以创建先前福彩12选5走势图,但实际上不使用该福彩12选5走势图。您正在丢弃您已经拥有的福彩12选5走势图 - 您知道,您将参数添加到 - 并告诉数据适配器创建新福彩12选5走势图。你永远不会向该福彩12选5走势图添加任何参数,所以为什么要在您被告知参数不足时会感到惊讶?

什么 you need to do is create a single command, add a parameter to it, execute that one command once and then get the result set. That means getting rid of the ExecuteNonQuery call for a start. It also mean either using the command you already have when creating the data adapter or letting the data adapter create the command first and then adding the parameter to that. There's also no point opening the connection because Fill will open it implicitly if it's not already open. It will also close it again if it had to open it, which is just as well, given that you code opens the connection but doesn't close it.

我也刚才意识到您使用现有连接创建福彩12选5走势图的代码,然后打开该连接,然后它创建一个新的连接对象!那是什么疯狂的?如果您未使用专用数据访问层(DAL),则在内部创建所有数据访问对象,您真的应该在需要时创建所有对象。创建连接对象,创建福彩12选5走势图和/或数据适配器对象,如果需要,请打开连接,执行SQL,如果需要关闭连接。简单而可重复。
C#:
private void SearchByName()
{
    var query = @"SELECT accid,
                         account.custid,
                         customer.title,
                         customer.firstname || ' ' || customer.lastname AS Name,
                         product.isaname AS ProductName,
                         balance,
                         accrued,
                         active
                  FROM account INNER JOIN customer
                  ON customer.custid = account.custid INNER JOIN product
                  ON product.prodid = account.prodid
                  WHERE Name = @Name";
    
    using (var connection = new SQLiteConnection(dbConnection.source))
    using (var adapter = new SQLiteDataAdapter(query, connection))
    {
        try
        {
            adapter.SelectCommand.Parameters.AddWithValue("@Name", txt_name.Text);
            dt_Accounts = new DataTable();
            da_Accounts.Fill(dt_Accounts);
            dgv_Account.DataSource = dt_Accounts;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
还有变体也可以工作,但代码简单且可读,并使用明智的自我记录名称。
 

罗兰斯富贸易

活跃的成员
加入
2019年2月3日
消息
42
地点
布里奇德
编程经验
Beginner
你真的应该花更多的时间学习Ado.net的基础知识,因为那里有很多错误。像这样的代码,当人们不考虑在写作之前实际要做什么时会发生什么。我不是在谈论最终结果,而是到达那里的步骤。如果您想到逻辑,并完成需要执行的步骤,那么编写代码以具体地实现这些步骤,您将不会结束代码做出无意义的东西或不执行所需的东西。让我们来看看那个代码,并考虑它所实际的东西。
C#:
using (SQLiteCommand cmd = connAccount.CreateCommand())
该行创建了一个新的福彩12选5走势图对象。好的。
C#:
cmd.CommandText = @"SELECT accid, account.custid, customer.title, customer.firstname || ' ' || customer.lastname AS Name, product.isaname AS ProductName, balance, accrued, active FROM account INNER JOIN customer ON customer.custid = account.custid INNER JOIN product ON product.prodid = account.prodid WHERE Name = @setName";
That line sets the SQL code for that command to a SELECT statement, i.e. a query. OK
C#:
cmd.Parameters.AddWithValue("@setName", txt_name.Text);
That line adds a parameter to the command. Why you would be using "@setName" instead of just "@Name" when the parameter is specifically to match the Name column is beyond me, but bad names will still work so we're still OK.
C#:
cmd.ExecuteNonQuery();
Now you have gone off the rails. This is what I mean about code doing pointless things. What exactly is that line supposed to be achieving? If you wrote an algorithm, what step would that be performing? The ExecuteNonQuery method is specifically for executing SQL statements that do not produce a result set, i.e. non-queries. What does a SELECT statement do? It produces a result set, i.e. it's a query. You obviously want the contents of that result set so what is the reason that you are calling ExecuteNonQuery? There isn;t one. You have code for no reason.
C#:
da_Accounts = new SQLiteDataAdapter(cmd.CommandText, connAccount);
在这里,您可以进一步了解轨道。您使用与使用相同的SQL查询创建数据适配器以创建先前福彩12选5走势图,但实际上不使用该福彩12选5走势图。您正在丢弃您已经拥有的福彩12选5走势图 - 您知道,您将参数添加到 - 并告诉数据适配器创建新福彩12选5走势图。你永远不会向该福彩12选5走势图添加任何参数,所以为什么要在您被告知参数不足时会感到惊讶?

什么 you need to do is create a single command, add a parameter to it, execute that one command once and then get the result set. That means getting rid of the ExecuteNonQuery call for a start. It also mean either using the command you already have when creating the data adapter or letting the data adapter create the command first and then adding the parameter to that. There's also no point opening the connection because Fill will open it implicitly if it's not already open. It will also close it again if it had to open it, which is just as well, given that you code opens the connection but doesn't close it.

我也刚才意识到您使用现有连接创建福彩12选5走势图的代码,然后打开该连接,然后它创建一个新的连接对象!那是什么疯狂的?如果您未使用专用数据访问层(DAL),则在内部创建所有数据访问对象,您真的应该在需要时创建所有对象。创建连接对象,创建福彩12选5走势图和/或数据适配器对象,如果需要,请打开连接,执行SQL,如果需要关闭连接。简单而可重复。
C#:
private void SearchByName()
{
    var query = @"SELECT accid,
                         account.custid,
                         customer.title,
                         customer.firstname || ' ' || customer.lastname AS Name,
                         product.isaname AS ProductName,
                         balance,
                         accrued,
                         active
                  FROM account INNER JOIN customer
                  ON customer.custid = account.custid INNER JOIN product
                  ON product.prodid = account.prodid
                  WHERE Name = @Name";
   
    using (var connection = new SQLiteConnection(dbConnection.source))
    using (var adapter = new SQLiteDataAdapter(query, connection))
    {
        try
        {
            adapter.SelectCommand.Parameters.AddWithValue("@Name", txt_name.Text);
            dt_Accounts = new DataTable();
            da_Accounts.Fill(dt_Accounts);
            dgv_Account.DataSource = dt_Accounts;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
还有变体也可以工作,但代码简单且可读,并使用明智的自我记录名称。
嘿,我真的很感激你戴上帽子,当我有机会和看看提供的链接时,我会透过它,到目前为止我所做的一切都是基于学院教学的,我正在慢慢地学习他们教的是什么都不是伟大的哈哈
 
最佳 底部