System.Data.SqlClient.SqlException: '必须声明标量变量"@SSN"



我正在用c#开发一个Windows窗体应用程序。我一直在SQL Server中加密列。

我的目标是在表单中从datagridview中提取数据并显示数据。

我想用where操作符拉数据并在datagridview中显示它,但我得到以下错误。有什么办法可以做到吗?

如果你能帮助我,我会很高兴的!误差

System.Data.SqlClient。SqlException: '必须声明标量变量"@SSN"

代码:

private void btnSearch_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source = " + IP + "; Initial Catalog = " + db + ";  Persist Security Info = False; User ID = " + username + "; Password = " + password + ";Column Encryption Setting = Enabled;");
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = @"SELECT* FROM AE WHERE TEST_TYPE = @SSN";
SqlParameter paramSSN = cmd.CreateParameter();
paramSSN.ParameterName = @"@SSN";
//paramSSN.ParameterName = "@SSN";
paramSSN.DbType = DbType.AnsiStringFixedLength;
paramSSN.Direction = ParameterDirection.Input;
paramSSN.Value = "'INITIAL_TEST'";
paramSSN.Size = 18;
DataSet data_set = new DataSet(cmd.CommandText);
dataAdapter = new SqlDataAdapter(cmd.CommandText,con);
SqlCommandBuilder commandbuild = new SqlCommandBuilder(dataAdapter);
dataAdapter.Fill(data_set);
dataGridView1.DataSource = data_set.Tables[0].DefaultView;
int rowCount = data_set.Tables[0].Rows.Count;
label6.Text = rowCount.ToString();//Total record 
con.Close();
}
}

当我像这样修改代码时,错误就消失了。我找到解决办法了。

ConnectionString();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = @"SELECT* FROM AE WHERE "+comboBox1.Text+" = 
@SSN";
SqlParameter paramSSN = cmd.CreateParameter();
DataSet data_set = new DataSet(cmd.CommandText);
dataAdapter = new SqlDataAdapter(cmd.CommandText,con);
dataAdapter.SelectCommand.Parameters.Add("@SSN", 
SqlDbType.VarChar,18).Value = textBox2.Text;
SqlCommandBuilder commandbuild = new SqlCommandBuilder(dataAdapter);
dataAdapter.Fill(data_set);
dataGridView1.DataSource = data_set.Tables[0].DefaultView;
int rowCount = data_set.Tables[0].Rows.Count;
label6.Text = rowCount.ToString();//Total record 
con.Close();
}

相关内容

  • 没有找到相关文章

最新更新