调用方法TextFileLoader时,报错:“ Tables 当前上下文中不存在名称“Tables” ”
找不到原因,请高手指教,不胜感激!源码如下:
public static DataSet TextFileLoader(string FilePath, string TableName, string[] FieldsInArray)
{
try
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=MesserDataLog;integrated security=True"); //打开数据库
conn.Open();
//执行查询语句
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from stx_1", conn); DataSet ds = new DataSet();
DataTable dt = new DataTable(TableName); //FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read);
FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs); //给DataTable赋初值
for (int i = 0; i < FieldsInArray.Length; i++)
{
dt.Columns.Add(new DataColumn(FieldsInArray[i], typeof(string)));
} string strRead;
bool flag = true; while (flag)
{
strRead = sr.ReadLine(); if (!string.IsNullOrEmpty(strRead))
{
//按照空格分隔文本的一行内容
string[] aryVale = strRead.Split(' ');
//code表示机器代码
string code;
//code表示log文件中的第2,3,4字符串的组合
code = aryVale[2] + aryVale[3] + aryVale[4];
//给DataTable新增加一行
DataRow dr = dt.NewRow();
string[] str1 = aryVale[0].Split('_');
string str2 = str1[1].Replace("-", ":");
string strTime = str1[0] +" "+ str2;
//转换成DateTime的形式
DateTime strTime1 = DateTime.Parse(strTime);
dr[FieldsInArray[2]] = strTime1;
dr[FieldsInArray[3]] = code;
if (code == "1240")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7-13字符串的组合
dr[FieldsInArray[8]] = aryVale[7] + aryVale[8] + aryVale[9] + aryVale[10] + aryVale[11] + aryVale[12] + aryVale[13]; }
if (code == "1213" && aryVale[7].Substring(1, 8) == "Machine")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7字符串的组合
dr[FieldsInArray[8]] = aryVale[7]; }
if (code == "1213"&&aryVale[7].Substring(1,8)=="Material")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7-10字符串的组合
dr[FieldsInArray[8]] = aryVale[7] + aryVale[8] + aryVale[9] + aryVale[10]; }
if (code == "1213" && aryVale[7].Substring(1, 10) == "PlanWeight")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7-10字符串的组合
dr[FieldsInArray[8]] = aryVale[7] + aryVale[8]; }
dt.Rows.Add(dr);
}
else
{
flag = false;
}
} ds.Tables.Add(dt);
return ds; } catch (Exception e)
{
throw e;
}
}
找不到原因,请高手指教,不胜感激!源码如下:
public static DataSet TextFileLoader(string FilePath, string TableName, string[] FieldsInArray)
{
try
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=MesserDataLog;integrated security=True"); //打开数据库
conn.Open();
//执行查询语句
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from stx_1", conn); DataSet ds = new DataSet();
DataTable dt = new DataTable(TableName); //FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read);
FileStream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs); //给DataTable赋初值
for (int i = 0; i < FieldsInArray.Length; i++)
{
dt.Columns.Add(new DataColumn(FieldsInArray[i], typeof(string)));
} string strRead;
bool flag = true; while (flag)
{
strRead = sr.ReadLine(); if (!string.IsNullOrEmpty(strRead))
{
//按照空格分隔文本的一行内容
string[] aryVale = strRead.Split(' ');
//code表示机器代码
string code;
//code表示log文件中的第2,3,4字符串的组合
code = aryVale[2] + aryVale[3] + aryVale[4];
//给DataTable新增加一行
DataRow dr = dt.NewRow();
string[] str1 = aryVale[0].Split('_');
string str2 = str1[1].Replace("-", ":");
string strTime = str1[0] +" "+ str2;
//转换成DateTime的形式
DateTime strTime1 = DateTime.Parse(strTime);
dr[FieldsInArray[2]] = strTime1;
dr[FieldsInArray[3]] = code;
if (code == "1240")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7-13字符串的组合
dr[FieldsInArray[8]] = aryVale[7] + aryVale[8] + aryVale[9] + aryVale[10] + aryVale[11] + aryVale[12] + aryVale[13]; }
if (code == "1213" && aryVale[7].Substring(1, 8) == "Machine")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7字符串的组合
dr[FieldsInArray[8]] = aryVale[7]; }
if (code == "1213"&&aryVale[7].Substring(1,8)=="Material")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7-10字符串的组合
dr[FieldsInArray[8]] = aryVale[7] + aryVale[8] + aryVale[9] + aryVale[10]; }
if (code == "1213" && aryVale[7].Substring(1, 10) == "PlanWeight")
{
//数据库中的"OPERATION_INFORMATION"字段对应log文件中7-10字符串的组合
dr[FieldsInArray[8]] = aryVale[7] + aryVale[8]; }
dt.Rows.Add(dr);
}
else
{
flag = false;
}
} ds.Tables.Add(dt);
return ds; } catch (Exception e)
{
throw e;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ string[] FieldsInArray ={ "JOB_ID", "DEVICE_CODE", "LAST_UPDATE_DATETIME", "NC_STAUS", "REMARK_TAG",
"TORCH_STATUS", "COMMAND_LINE_NUMBER", "OPERATION_INFORMATION",
"NESTING_PLATE", "OPERATION_START_TIME", "OPERATION_END_TIME",
"OPERATION_LENGTH", "PARTNAME", "PARTNUMBERS", "BK1", "BK2", "BK3", "BK4", "BK5"}; DataSet ds = new DataSet(); ds = TextFileLoader(@"c:\5TT-Log.TXT", "stx_1", FieldsInArray); DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = ds.Tables[0];
}
在这句“ds.Tables.Add(dt);”报的错
dataGridView1.DataSource = DataTable; //返回一个DataTable类型的数据源
更省事撒
/// <summary>
/// 根据SQL语句和参数查询,返回DataTable集合
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns></returns>
public DataTable GetDataSet(string sql, params SqlParameter[] param)
{
DataSet ds = new DataSet();
SqlCommand comm = new SqlCommand(sql, Conn);
comm.Parameters.AddRange(param);
SqlDataAdapter sda = new SqlDataAdapter(comm);
sda.Fill(ds);
DataTable table = ds.Tables[0];
Conn.Close();
return table; }这个是我以前写的,然后直接放这个获取出来的数据源懂了不?
if (hC_all_dt.Tables[0].Rows.Count > 0)
{
DataTable hC_all_new = new DataTable();
for (int i = 0; i < hC_all_dt.Tables[0].Rows.Count; i++)
{
if (Convert.ToInt32(hC_all_dt.Tables[0].Rows[i]["N_MA1"].ToString()) == 0)
{
hC_all_dt.Tables[0].Rows.Remove(hC_all_dt.Tables[0].Rows[i]);
}
}
hC_all_new = hC_all_dt.Tables[0];
this.JXGrid1.DataSource = hC_all_new;
参考参考