SqlDataAdapter 句柄错误 - 未创建对象
SqlDataAdapter handle error - object is not created
我有这个错误:
Invalid object name 'sap.AfdelingsrapportACTUAL'.
这是因为这个 table 还没有创建。
我需要在我的 SqlDataAdapter
中实施检查 - 所以如果为 null 或没有得到任何东西,它就会继续。但是我真的不知道怎么办。
I think it should be around my SqlDataAdapter but i could be wrong
代码
DataTable sqldt = new DataTable();
string sqlQuery = @"Select * from " + table;
SqlConnection sqlcon = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sqlQuery, sqlcon);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(sqldt);
}
我认为应该是这样的:
public static bool CompareDataTables(DataTable dt, string table, string connectionString)
{
DataTable sqldt = new DataTable();
string sqlQuery = @"Select * from " + table;
SqlConnection sqlcon = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sqlQuery, sqlcon);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
if (da == "doesnt have any table")
{
return true;
}
else
{
da.Fill(sqldt);
}
}
int sqlCols = sqldt.Columns.Count;
int excelCols = dt.Columns.Count;
if (excelCols == sqlCols)
{
return false;
}
else return true;
}
一种方法是首先检查数据是否存在 table。
按照此处给出的示例进行操作:
Check if table exists in SQL Server
我有这个错误:
Invalid object name 'sap.AfdelingsrapportACTUAL'.
这是因为这个 table 还没有创建。
我需要在我的 SqlDataAdapter
中实施检查 - 所以如果为 null 或没有得到任何东西,它就会继续。但是我真的不知道怎么办。
I think it should be around my SqlDataAdapter but i could be wrong
代码
DataTable sqldt = new DataTable();
string sqlQuery = @"Select * from " + table;
SqlConnection sqlcon = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sqlQuery, sqlcon);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(sqldt);
}
我认为应该是这样的:
public static bool CompareDataTables(DataTable dt, string table, string connectionString)
{
DataTable sqldt = new DataTable();
string sqlQuery = @"Select * from " + table;
SqlConnection sqlcon = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sqlQuery, sqlcon);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
if (da == "doesnt have any table")
{
return true;
}
else
{
da.Fill(sqldt);
}
}
int sqlCols = sqldt.Columns.Count;
int excelCols = dt.Columns.Count;
if (excelCols == sqlCols)
{
return false;
}
else return true;
}
一种方法是首先检查数据是否存在 table。
按照此处给出的示例进行操作: Check if table exists in SQL Server