【问题标题】:Simple select C# command - Beginner简单的选择 C# 命令 - 初学者
【发布时间】:2021-12-05 11:52:51
【问题描述】:

你能告诉我哪里出错了吗?我认为这与我声明的变量有关,但我不确定

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 WindowsFormsApp4
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=xxx.xxx.xxx.xxx;Initial 
Catalog=VVM;Persist Security Info=True;User ID=sa;Password=xxxxx");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select dExpiryDate from tblStock where 
Stock_strBarcode= @Barcode", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        cmd.Parameters.AddWithValue("@Barcode", textBox1.Text);

        cmd.ExecuteNonQuery();

        con.Close();
        MessageBox.Show("Expiry Dates Updated! ;) ");
    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void label4_Click(object sender, EventArgs e)
    {

    }
}
}

我得到的例外是

    "System.Data.SqlClient.SqlException
    HResult=0x80131904
    Message=Must declare the scalar variable "@Barcode".
    Source=.Net SqlClient Data Provider
    StackTrace:
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean 
    breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean 
    breakConnection, Action`1 wrapCloseInAction)
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, 
    Boolean callerHasConnectionLock, Boolean asyncClose)
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, 
    SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject 
    stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior 

【问题讨论】:

  • 您的代码还有其他问题:需要使用using 块处理连接和适配器。 cmd.ExecuteNonQuery(); 完全是假的,应该删除。 AddWithValue is Evil,明确指定参数的类型和长度。不要硬编码连接字符串。如果Stock_strBarcode 是唯一的并且您只能得到一行、一列,那么请考虑改用cmd.ExecuteScalar

标签: c# sql asp.net .net system.data


【解决方案1】:

你必须在使用前将参数添加到命令中
这条线应该上移

cmd.Parameters.AddWithValue("@Barcode", textBox1.Text);

试试这个:

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 WindowsFormsApp4
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=xxx.xxx.xxx.xxx;Initial 
Catalog=VVM;Persist Security Info=True;User ID=sa;Password=xxxxx");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select dExpiryDate from tblStock where 
Stock_strBarcode= @Barcode", con);
cmd.Parameters.AddWithValue("@Barcode", textBox1.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        

        cmd.ExecuteNonQuery();

        con.Close();
        MessageBox.Show("Expiry Dates Updated! ;) ");
    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void label4_Click(object sender, EventArgs e)
    {

    }
}
}

【讨论】:

  • 你的传奇。谢谢;)
  • 祝你好运@Martin
猜你喜欢
  • 2020-07-13
  • 2017-04-10
  • 2013-10-18
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 2014-06-21
相关资源
最近更新 更多