简单选择 C# 命令 - 初学者

Simple select C# command - Beginner

提问人:Martin 提问时间:10/18/2021 最后编辑:jpsMartin 更新时间:10/18/2021 访问量:298

问:

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

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 
C# SQL asp.net NET System.data

评论

1赞 Charlieface 10/18/2021
你的代码还有其他问题:需要用块来处理连接和适配器。 完全是假的,应该删除。AddWithValue 为 Evil,请显式指定参数的类型和长度。不要对连接字符串进行硬编码。如果是唯一的,并且您只得到一行、一列,则考虑改用usingcmd.ExecuteNonQuery();Stock_strBarcodecmd.ExecuteScalar

答:

1赞 Milad Dastan Zand 10/18/2021 #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)
    {

    }
}
}

评论

0赞 Martin 10/18/2021
你这个传奇。谢谢;)
0赞 Milad Dastan Zand 10/18/2021
祝你好运@Martin