C# 与SqlDataAdapter.Fill和SqlDataReader.Read的结果不一致
我有一个SQL数据库,其中包含我想要读入DataTable对象的各种表。为此,我使用了SqlDataAdapter.Fill,它可以完美地处理除一个名为“Problems”的表之外的所有表。在这个表上,该方法运行时不会抛出任何异常,但结果DataTable只有3321行,而SQL表有9800行。“问题”表的代码与其他表的代码相同:C# 与SqlDataAdapter.Fill和SqlDataReader.Read的结果不一致,c#,mysql,sql-server,datatable,C#,Mysql,Sql Server,Datatable,我有一个SQL数据库,其中包含我想要读入DataTable对象的各种表。为此,我使用了SqlDataAdapter.Fill,它可以完美地处理除一个名为“Problems”的表之外的所有表。在这个表上,该方法运行时不会抛出任何异常,但结果DataTable只有3321行,而SQL表有9800行。“问题”表的代码与其他表的代码相同: adapter.SelectCommand = new SqlCommand("SELECT * FROM [Problems]", my_connection);
adapter.SelectCommand = new SqlCommand("SELECT * FROM [Problems]", my_connection);
adapter.Fill(my_dataset, "Problems");
我听了adapter.FillError和adapter.Row更新的错误状态,但什么也没听到。因此,我决定使用SqlDataReader(下面的代码)按顺序读取该表。这样做效果很好,占满了9800行。这使我担心使用SqlDataAdapter.Fill可能会丢失其他表的数据,因此我推广了为SqlDataReader编写的方法,以允许我按顺序读取任何表(下面的代码也包括在内)。但当我尝试使用这个广义方法时,我又只得到了3321行
我希望能够将SQL数据库读入数据表,而不必担心丢失数据
“问题”的顺序读取代码(这是实际工作的代码):
接下来是上述方法的推广,在“Problems”表中,该方法似乎以与SqlDataAdapter.Fill方法相同的方式失败。在我的代码中,我通过
my_dataset.Tables.Add(GetTable("Problems", "SELECT * FROM [Problems]", new Tuple<string, Type>("ExerciseID", typeof(int)), new Tuple<string, Type>("ParentID", typeof(int)), new Tuple<string, Type>("AnswerFields", typeof(string))));
添加(GetTable(“Problems”,“SELECT*FROM[Problems]”)、新元组(“ExerciseID”,typeof(int))、新元组(“ParentID”,typeof(int))、新元组(“AnswerFields”,typeof(string)));
方法本身是
public static DataTable GetTable(string tableName, string selectCommand, params Tuple<string, Type>[] fields)
{
SqlCommand com = new SqlCommand(selectCommand, ProblemDBConnection());
if(com==null)
throw new Exception("WTF");
if(fields.Length == 0)
throw new Exception("WTF");
DataTable table = new DataTable(tableName);
DataColumn dc;
for(int i=0; i<fields.Length; i++)
{
if(fields[i].Item2 == typeof(string))
dc = new DataColumn(fields[i].Item1, typeof(string));
else
{
if(fields[i].Item2 != typeof(int))
throw new Exception("WTF");
dc = new DataColumn(fields[i].Item1, typeof(int));
}
table.Columns.Add(dc);
}
table.PrimaryKey = new DataColumn[] {table.Columns[0]};
com.Connection.Open();
SqlDataReader rdr = com.ExecuteReader();
DataRow dr;
while (rdr.Read())
{
dr = table.NewRow();
for (int i = 0; i < fields.Length; i++)
{
if (fields[i].Item1 == "AnswerFields")
{
if (DBNull.Value.Equals(rdr["Answers"]))
dr[fields[i].Item1] = "";
else
dr[fields[i].Item1] = ProblemAnswerFields(rdr["Answers"].ToString());
}
else if (DBNull.Value.Equals(rdr[fields[i].Item1]))
{
if (fields[i].Item2 == typeof(string))
dr[fields[i].Item1] = "";
else
{
dr[fields[i].Item1] = -1;
}
}
else
{
if (fields[i].Item2 == typeof(string))
dr[fields[i].Item1] = rdr[fields[i].Item1].ToString();
else
{
dr[fields[i].Item1] = (int)rdr[fields[i].Item1];
}
}
}
table.Rows.Add(dr);
}
rdr.Close();
com.Connection.Close();
return table;
}
publicstaticdatatable GetTable(stringtablename、stringselectcommand、params Tuple[]字段)
{
SqlCommand com=newsqlcommand(selectCommand,ProblemDBConnection());
如果(com==null)
抛出新异常(“WTF”);
如果(fields.Length==0)
抛出新异常(“WTF”);
DataTable=新的DataTable(tableName);
数据列dc;
对于(int i=0;iNevermind),在失败的情况下,我意外地使用了一个旧连接,从一个不推荐的数据库版本中提取了所有数据。><
public static DataTable GetTable(string tableName, string selectCommand, params Tuple<string, Type>[] fields)
{
SqlCommand com = new SqlCommand(selectCommand, ProblemDBConnection());
if(com==null)
throw new Exception("WTF");
if(fields.Length == 0)
throw new Exception("WTF");
DataTable table = new DataTable(tableName);
DataColumn dc;
for(int i=0; i<fields.Length; i++)
{
if(fields[i].Item2 == typeof(string))
dc = new DataColumn(fields[i].Item1, typeof(string));
else
{
if(fields[i].Item2 != typeof(int))
throw new Exception("WTF");
dc = new DataColumn(fields[i].Item1, typeof(int));
}
table.Columns.Add(dc);
}
table.PrimaryKey = new DataColumn[] {table.Columns[0]};
com.Connection.Open();
SqlDataReader rdr = com.ExecuteReader();
DataRow dr;
while (rdr.Read())
{
dr = table.NewRow();
for (int i = 0; i < fields.Length; i++)
{
if (fields[i].Item1 == "AnswerFields")
{
if (DBNull.Value.Equals(rdr["Answers"]))
dr[fields[i].Item1] = "";
else
dr[fields[i].Item1] = ProblemAnswerFields(rdr["Answers"].ToString());
}
else if (DBNull.Value.Equals(rdr[fields[i].Item1]))
{
if (fields[i].Item2 == typeof(string))
dr[fields[i].Item1] = "";
else
{
dr[fields[i].Item1] = -1;
}
}
else
{
if (fields[i].Item2 == typeof(string))
dr[fields[i].Item1] = rdr[fields[i].Item1].ToString();
else
{
dr[fields[i].Item1] = (int)rdr[fields[i].Item1];
}
}
}
table.Rows.Add(dr);
}
rdr.Close();
com.Connection.Close();
return table;
}