在C#中如何将数据库数据访问到组合框中 [英] In C# how to access database data into combobox

查看:129
本文介绍了在C#中如何将数据库数据访问到组合框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用sql server数据库创建了包含用户名和密码的登录页面,我创建了一个存储用户名的sting,然后使用用户名的字符串在下一个,下一个,下一个Windows应用程序(.NET)中创建。我想要访问该行中的数据而不是密码所有剩余的数据都应该显示在组合框中。下面的代码是错误的,因为我完全不知道它。



st =用户名



i = 1-30,因为我的专栏命名为1到30



请给我一个提示或代码或想法或者......



我尝试过:



int i;

string [] s;

private void label1_Click(object sender,EventArgs e)

{

st = label1.Text;



SqlConnection con = new SqlConnection(@Data Source =(LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\Users\psais\Docum ents\sghsdatabase.mdf; Integrated Security = True; Connect Timeout = 30);



string q = st;



SqlCommand cmd = new SqlCommand(q,con);



SqlDataReader r;



尝试

{

con.Open();

r = cmd.ExecuteReader();

while (r.Read()){

for(i = 1;我< = 30; i ++)

{

s [i] = r.GetString(i);

}

for(i = 1; i <= 30; i ++)

{

comboBox1.Items.Add(s [i]);

}



}

}

I have created login page containing username and password using sql server data base and i created a sting which stores username and then in next,next,next windows application(.NET) by using the string which is username .I wanted to access data in that row not password all the remaining data should be displayed in combo box.Below is the code there is mistake for sure because i don't know about it perfectly.

st=username

i=1-30 , Because my column are named 1 to 30

please send me a hint or code or idea or...

What I have tried:

int i;
string[] s;
private void label1_Click(object sender, EventArgs e)
{
st = label1.Text;

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\psais\Documents\sghsdatabase.mdf;Integrated Security=True;Connect Timeout=30");

string q = st;

SqlCommand cmd = new SqlCommand(q,con);

SqlDataReader r;

try
{
con.Open();
r = cmd.ExecuteReader();
while (r.Read()) {
for (i = 1; i <= 30; i++)
{
s[i] = r.GetString(i);
}
for (i = 1; i <= 30; i++)
{
comboBox1.Items.Add("s[i]");
}

}
}

推荐答案

引用:

肯定有错误



如果这是你的代码那么就会出现很多错误,但是主要错误是没有意义的代码,因为你没有读者的SQL命令。如果您尝试复制登录系统,则需要一个带有表的简单数据库来保存用户信息。创建一个具有有意义名称的表,以便您知道每列代表什么。首先,您需要用户ID和密码,以及用户要输入的任何其他详细信息。当用户尝试访问系统时,您将查找数据库以查看它们是否已注册。如果没有,请转到注册页面。如果他们已经注册,那么您只需要检查密码 - 并且不要将密码保存为明文(甚至加密)文本。


If that is your code then there are a lot of mistakes, but the main one is that the code makes no sense, because you do not have a SQL command for the reader. If you are trying to replicate a login system then you need a simple database with a table to hold the user information. Create a table with meaningful names so you know what each column represents. As a start you will need the userid and the password, plus any other details the user is to input. When a user tries to access the system you do a lookup of the database to see if they are registered. If not, go to the registration pages. If they are registered then you just need to check the password - and do not save passwords in clear (or even encrypted) text.


我会回应Richard的评论。从来没有命名列1到30.您的表的字段名称需要有有意义的名称,否则您将花费​​大量时间检查数据库,试图记住您在第17列中的内容。我会说同样的关于表格控制的事情。而不是comboBox1你想要cbUserData



至于你的具体问题,问题是你没有告诉命令你在寻找什么。关注此

I would echo Richard's comments. Never ever ever name columns 1 to 30. The field names of your tables need to have meaningful names or you'll spend a lot of time checking back to the database trying to remember what you put in column 17. I'd say the same thing about controls on forms. Rather than comboBox1 you want cbUserData

As to your specific question the problem is you didn't tell the command what you're looking for. Follow this
st=username
string q = st
SqlCommand cmd = new SqlCommand(q,con) // at this point q == username



所以你说的是SqlCommand(用户名,骗局),如果他们输入johndoe作为用户名然后是SqlCommand('johndoe', con)不足以弄清楚你想要什么。 'q'必须是一个SQL语句,如SELECT * FROM User WHERE UserName =+ username



我对这里的用户体验也有疑问。您想在数据库中找到单个记录,然后在组合框中显示您在该用户上拥有的所有其他数据,如PersonId,FirstName,MiddleName,LastName,DOB,HairColor等等?



还有几条评论,你可以在一个单独的下一个循环中进行这两项操作。将值赋入s [i]不会干扰将s [i]中的值放入组合框中,因此您可以将它们放入同一个循环中。而你实际上是将字符串s [i]添加到组合框中30次,而不是实际值。


So what you're saying is SqlCommand(username, con) if they type in johndoe as username then SqlCommand('johndoe', con) isn't enough to figure out what you want. 'q' has to be a SQL statement like "SELECT * FROM User WHERE UserName = " + username

I also have a question about the user experience here. You want to find a single record in the database and then display in a combo box all the other data that you have on that user, like PersonId, FirstName, MiddleName, LastName, DOB, HairColor, etc etc?

A couple more comments, you can do both operations in a single for next loop. Getting the value into s[i] doesn't interfere with putting the value from s[i] into the combo box so you can just put them into the same loop. And you're actually adding the string "s[i]" into the combo box 30 times, rather than the actual value.

for (i=1; i <= 30; i++)
{
    s[i] = r.GetString(i);
    cbUserData.Items.Add(s[i]);
}



还有一件事......对不起,不要吝啬。用<代码包围你的代码。前> < / pre>标签(没有空格,我不得不添加它们以使其可见)这样它将保留语法和缩进。



HTH,

Mike


One more thing.. sorry, not trying to be mean. Surround your code with < pre > < / pre > tags (without the spaces, I had to add them to make it visible) That way it will preserve syntax and indentations.

HTH,
Mike


这篇关于在C#中如何将数据库数据访问到组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆