如何将SQL中特定列的数据导入C#中的标签? [英] How do I import data from a particular column in SQL to label in C#?
问题描述
您好,
我正在尝试从SQL Server中的表中获取数据到我的Windows窗体中的Label。它显示我一个空的异常错误,我无法弄清楚原因。
我收到此错误:
System.IndexOutOfRangeException:'@Name'位于以下行:
NameLabel.Text = reader [@ Name]。ToString();
我将它存储在字符串中,因为我以另一种形式调用该值。
请帮助。
我的尝试:
Hello,
I am trying to fetch data from a table in SQL Server to Label in my Windows Form. It is showing me a null exception error and I am unable to figure out why.
I am getting this error:
System.IndexOutOfRangeException: '@Name' on the following line:
NameLabel.Text = reader["@Name"].ToString();
I am storing it in a string because I am calling the value in another form.
Please help.
What I have tried:
string queryText = @"SELECT Count(*) FROM NewsClip_Login
WHERE Username = @Username AND Password = @Password";
using (SqlConnection cn = new SqlConnection("Data Source=PRIMO-CHALICE;Initial Catalog=NewsClip;Integrated Security=True"))
using (SqlCommand cmd = new SqlCommand(queryText, cn))
{
cn.Open();
cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text);
cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text);
cmd.Parameters.AddWithValue("@Name", NameLabel.Text);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
NameLabel.Text = reader["@Name"].ToString();
NameLabelText = NameLabel.Text;
reader.Close();
int result = (int)cmd.ExecuteScalar();
if (result > 0)
{
this.Hide();
NewsClip_Main nc_main = new NewsClip_Main();
nc_main.Enabled = true;
nc_main.ShowDialog();
this.Close();
}
else
MessageBox.Show("User Not Found!");
cn.Close();
推荐答案
您没有名为的参数@name
在您的SQL语句中。也许你想选择它像
You don't have a parameter called@name
in your SQL statement. Perhaps you wanted to select it like
SELECT Name FROM NewsClip_Login WHERE Username = @Username AND Password = @Password";
如果您正在尝试这样做,那么您可以参考结果列wiuthout @,因为它用于参数。
关于密码的另一件事。永远不要将密码存储为纯文本。它们应始终单向散列。请查看密码存储:怎么做。 [ ^ ]
ADDITION
--------- -
If that is what you're trying to do then you refer to the result columns wiuthout @ since that is used for parameters.
Another thing concerning the password. Never ever store the passwords as plain text. They should always be hashed one-way. Have a look at Password Storage: How to do it.[^]
ADDITION
-----------
string queryText = @"SELECT Name FROM NewsClip_Login WHERE Username = @Username AND Password = @Password";
using (SqlConnection cn = new SqlConnection("Data Source=PRIMO-CHALICE;Initial Catalog=NewsClip;Integrated Security=True")) {
using (SqlCommand cmd = new SqlCommand(queryText, cn))
{
cn.Open();
cmd.Parameters.AddWithValue("@Username", UsernameTextBox.Text);
cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
NameLabel.Text = reader["Name"].ToString();
NameLabelText = NameLabel.Text;
reader.Close();
...
抱歉,我没有意识到您的SQL语句没有名称列
Sorry I didn't realize that your SQL statement not having Name column
SELECT Count(*) FROM NewsClip_Login
如果您正在使用阅读器,则应该具有列名,例如,如果您要获取值那么你应该在你的select语句中有name列。
从阅读器中删除@它应该是reader [Name]
You should have columnname if you are using reader for example if you are fetching the value of name then you should have name column in your select statement.
Remove @ from the reader it should be reader ["Name"]
这篇关于如何将SQL中特定列的数据导入C#中的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!