简单 select C# 命令 - 初学者
Simple select C# command - Beginner
你能告诉我哪里错了吗?我认为这与我声明的变量有关,但我不确定
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
您必须在命令中添加参数才能使用它
此行应上移
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)
{
}
}
}
你能告诉我哪里错了吗?我认为这与我声明的变量有关,但我不确定
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
您必须在命令中添加参数才能使用它
此行应上移
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)
{
}
}
}