I'd like to populate an User array with data from my SQL Server database using a SqlDataReader
.
我想使用SqlDataReader使用我的SQL Server数据库中的数据填充User数组。
This is my code so far:
到目前为止这是我的代码:
public struct User
{
public int id;
public string log;
public string password;
public User (int id1,string s, s2)
{
id=id1;
log =s;
password=s2;
}
}
User[] al = new User[50];
int i=0;
using (SqlConnection connection = new SqlConnection("string")
{
connection.Open();
SqlCommand command = new SqlCommand("Select [UserName], [Password]. from [TaUser]", connection);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// populate the al array with the datas from the 3 columns : ID, UserName, Password
}
}
connection.Close();
}
I know that if I had a simple arraylist I could just do al.Add("")
, however, I struggle when it comes to struct arrays.
我知道如果我有一个简单的arraylist我可以做al.Add(“”),但是,当涉及到struct数组时,我很难。
2 个解决方案
#1
1
There are a lots of errors in your code.
您的代码中存在大量错误。
First, your User constructor is invalid, it should be:
首先,您的User构造函数无效,它应该是:
public User(int id1, string s, string s2)
Second, your query does not return user id.
其次,您的查询不返回用户ID。
Third, it would probably be better to use List instead of array.
第三,使用List而不是数组可能会更好。
With all that, this should work
尽管如此,这应该有效
List<User> userList = new List<User>() ;
using (SqlConnection connection = new SqlConnection("string")
{
connection.Open();
SqlCommand command = new SqlCommand("Select [Id], [UserName], [Password]. from [TaUser]", connection);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var id = reader.GetInt32(0);
var userName = reader.GetString(1);
var pwd = reader.GetString(2);
var user = new User(id, userName, pwd);
userList.Add(user);
}
}
connection.Close();
// if you really need an array, do it here
var al = userList.ToArray()
#2
0
I'd advise doing something like this:
我建议做这样的事情:
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
Then read out of that DataTable like this:
然后读出那个DataTable,如下所示:
string name = dataTable.Rows[0]["UserName"] as string;
Then fill your User struct with the gathered info. Job Done?
然后使用收集的信息填充User结构。任务完成?
#1
1
There are a lots of errors in your code.
您的代码中存在大量错误。
First, your User constructor is invalid, it should be:
首先,您的User构造函数无效,它应该是:
public User(int id1, string s, string s2)
Second, your query does not return user id.
其次,您的查询不返回用户ID。
Third, it would probably be better to use List instead of array.
第三,使用List而不是数组可能会更好。
With all that, this should work
尽管如此,这应该有效
List<User> userList = new List<User>() ;
using (SqlConnection connection = new SqlConnection("string")
{
connection.Open();
SqlCommand command = new SqlCommand("Select [Id], [UserName], [Password]. from [TaUser]", connection);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var id = reader.GetInt32(0);
var userName = reader.GetString(1);
var pwd = reader.GetString(2);
var user = new User(id, userName, pwd);
userList.Add(user);
}
}
connection.Close();
// if you really need an array, do it here
var al = userList.ToArray()
#2
0
I'd advise doing something like this:
我建议做这样的事情:
SqlDataReader dataReader = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dataReader);
Then read out of that DataTable like this:
然后读出那个DataTable,如下所示:
string name = dataTable.Rows[0]["UserName"] as string;
Then fill your User struct with the gathered info. Job Done?
然后使用收集的信息填充User结构。任务完成?