c# SqlDataAdapter 没有 return 结果
c# SqlDataAdapter doesn't return results
出于某种原因,我的函数有时不会 return 出于某种原因的查询结果,尽管它应该(运行 它在 SQL Studio 中)。我知道代码不是很干净而且有点奇怪,但它有时可以工作。想不通了。
BestellNo 可以作为整数或字符串输入查询中。
我想问题可能出在这条线上?
if (dsUniversal.Tables[0].Rows.Count > 0)
string HA_No = "SELECT [A-Nr], B, C, [D-Nr], E FROM Table WHERE (BestellNr = '" + BestellNo + "')";
private string[] getStringData(string strSQL, string conn)
{
string ret = "";
DataSet dsUniversal = new DataSet();
System.Data.SqlClient.SqlConnection sConn = new System.Data.SqlClient.SqlConnection(conn);
if (sConn.State == 0)
sConn.Open();
System.Data.SqlClient.SqlDataAdapter daUniversal = new System.Data.SqlClient.SqlDataAdapter(strSQL, sConn);
daUniversal.MissingSchemaAction = MissingSchemaAction.AddWithKey;
try
{
daUniversal.Fill(dsUniversal);
}
catch (System.Data.SqlClient.SqlException sEx)
{
sConn.Close();
ret = "Fehler";
logIt("SQL Fehler (" + DateTime.Now.ToString() + "): " + sEx.Message, "Universal");
string[] err = { ret, ret, ret, ret, ret };
return err;
}
sConn.Close();
if (dsUniversal.Tables[0].Rows.Count > 0)
{
string var1 = dsUniversal.Tables[0].Rows[0].ItemArray[0].ToString();
string var2 = dsUniversal.Tables[0].Rows[0].ItemArray[1].ToString();
string var3 = dsUniversal.Tables[0].Rows[0].ItemArray[2].ToString();
string var4 = dsUniversal.Tables[0].Rows[0].ItemArray[3].ToString();
string var5 = dsUniversal.Tables[0].Rows[0].ItemArray[4].ToString();
string[] res = { var1, var2, var3, var4, var5 };
return res;
}
else
{
string var1 = "";
string var2 = "";
string var3 = "";
string var4 = "";
string var5 = "";
string[] res = { var1, var2, var3, var4, var5 };
return res;
}
}
如果只是读出数据,可以不用DataSet
、DateTable
、SqlDataAdapter
等,直接用SqlDataReader
取数据即可完成它。
private static string[] ReadData(string queryString, string connectionString)
{
var result = new List<string>();
using (var connection =
new SqlConnection(connectionString))
{
var command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
// Please note I am just reading the value of the first column
// so if you want other columns, you would need to modify this
// to suit your needs.
result.Add(reader[0].ToString());
}
// Call Close when done reading.
reader.Close();
}
return result.ToArray();
}
出于某种原因,我的函数有时不会 return 出于某种原因的查询结果,尽管它应该(运行 它在 SQL Studio 中)。我知道代码不是很干净而且有点奇怪,但它有时可以工作。想不通了。
BestellNo 可以作为整数或字符串输入查询中。
我想问题可能出在这条线上?
if (dsUniversal.Tables[0].Rows.Count > 0)
string HA_No = "SELECT [A-Nr], B, C, [D-Nr], E FROM Table WHERE (BestellNr = '" + BestellNo + "')";
private string[] getStringData(string strSQL, string conn)
{
string ret = "";
DataSet dsUniversal = new DataSet();
System.Data.SqlClient.SqlConnection sConn = new System.Data.SqlClient.SqlConnection(conn);
if (sConn.State == 0)
sConn.Open();
System.Data.SqlClient.SqlDataAdapter daUniversal = new System.Data.SqlClient.SqlDataAdapter(strSQL, sConn);
daUniversal.MissingSchemaAction = MissingSchemaAction.AddWithKey;
try
{
daUniversal.Fill(dsUniversal);
}
catch (System.Data.SqlClient.SqlException sEx)
{
sConn.Close();
ret = "Fehler";
logIt("SQL Fehler (" + DateTime.Now.ToString() + "): " + sEx.Message, "Universal");
string[] err = { ret, ret, ret, ret, ret };
return err;
}
sConn.Close();
if (dsUniversal.Tables[0].Rows.Count > 0)
{
string var1 = dsUniversal.Tables[0].Rows[0].ItemArray[0].ToString();
string var2 = dsUniversal.Tables[0].Rows[0].ItemArray[1].ToString();
string var3 = dsUniversal.Tables[0].Rows[0].ItemArray[2].ToString();
string var4 = dsUniversal.Tables[0].Rows[0].ItemArray[3].ToString();
string var5 = dsUniversal.Tables[0].Rows[0].ItemArray[4].ToString();
string[] res = { var1, var2, var3, var4, var5 };
return res;
}
else
{
string var1 = "";
string var2 = "";
string var3 = "";
string var4 = "";
string var5 = "";
string[] res = { var1, var2, var3, var4, var5 };
return res;
}
}
如果只是读出数据,可以不用DataSet
、DateTable
、SqlDataAdapter
等,直接用SqlDataReader
取数据即可完成它。
private static string[] ReadData(string queryString, string connectionString)
{
var result = new List<string>();
using (var connection =
new SqlConnection(connectionString))
{
var command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
// Please note I am just reading the value of the first column
// so if you want other columns, you would need to modify this
// to suit your needs.
result.Add(reader[0].ToString());
}
// Call Close when done reading.
reader.Close();
}
return result.ToArray();
}