"编号" "日期" "时间" "考勤机号"
"00000" "03-11-14" "00:00:00" "1"
"00002" "03-11-14" "10:29:34" "1"
"00002" "03-11-14" "10:33:02" "1"
"00157" "03-11-14" "10:34:24" "1"
"00157" "03-11-14" "10:34:44" "1"
"00157" "03-11-14" "10:35:31" "1"
"00157" "03-11-14" "10:37:31" "1"
"00002" "03-11-14" "10:48:31" "1"
"00157" "03-11-14" "12:16:04" "1"
"00157" "03-11-14" "12:17:19" "1"
"00157" "03-11-14" "12:17:51" "1"
"00002" "03-11-14" "10:18:09" "1"
"00002" "03-11-14" "10:30:53" "1"
"00157" "03-11-14" "10:34:22" "1"
"00157" "03-11-14" "10:34:26" "1"
"00157" "03-11-14" "10:35:12" "1"
"00157" "03-11-14" "10:35:54" "1"
"00157" "03-11-14" "10:38:27" "1"
"00002" "03-11-14" "12:05:31" "1"
"00157" "03-11-14" "12:17:06" "1"
"00157" "03-11-14" "12:17:36" "1"
看看这段代码就可以了,类似 private System.IO.StreamReader GetStreamReader()
{
HttpPostedFile hpf = fileData.PostedFile;
System.IO.Stream stream = hpf.InputStream;

System.IO.StreamReader sr = new System.IO.StreamReader(stream);
return sr;
} private void LoadCardRecord()
{
string strText = "";
string strCurLine = "";
int rows = 0;

System.IO.StreamReader sr = GetStreamReader(); strCurLine = sr.ReadLine();
txtFileContent.Value = "";
while(strCurLine != null && strCurLine != "")
{
strText += strCurLine;
txtFileContent.Value += strCurLine + "\n";
strCurLine = sr.ReadLine();
rows++;
}
txtFileContent.Rows = rows;
sr.Close();
} private void Save()
{
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
try
{
WebMIS.Web.Service.Web w = new WebMIS.Web.Service.Web();
conn.ConnectionString = w.ConnectionString;
conn.Open();
}
catch
{
Response.Write("<script>alert('打开数据库失败!');</script>");
return;
}

System.IO.StreamReader sr = GetStreamReader();
bool blnHasData = false; if (sr != null)
{
string strCurLine = ""; //当前行记录文本
string strSQL = ""; //保存当前记录的SQL
string[] arrCurLine; //当前行记录以Tab键分隔的文本数组
int count = 0; /*
"编号" "日期" "时间" "考勤机号"
"00000" "03-11-14" "00:00:00" "1"
"00002" "03-11-14" "10:29:34" "1"
"00002" "03-11-14" "10:33:02" "1"
*/
strCurLine = sr.ReadLine(); //第一行为标题 strCurLine = sr.ReadLine(); //第二行开始为考勤数据
if (strCurLine != null)
{
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = conn; while(strCurLine != null && strCurLine != "")
{
try
{
arrCurLine = strCurLine.Split('\t');
strSQL = "Insert Into CardRecord(ID,行值,编号,日期,时间,考勤机号";
strSQL += ") Values (";
strSQL += "'" + System.Guid.NewGuid(); //ID
strSQL += "','" + strCurLine; //行值,把整个写到一个字段
strSQL += "','" + arrCurLine[0].Substring(1,arrCurLine[0].Length-2);
strSQL += "','" + arrCurLine[1].Substring(1,arrCurLine[1].Length-2);
strSQL += "','" + arrCurLine[2].Substring(1,arrCurLine[2].Length-2);
strSQL += "','" + arrCurLine[3].Substring(1,arrCurLine[3].Length-2);
strSQL += "')";
/*
纪录结构:
0:      卡类型
1、2:   卡流水号,1为低位元,2为高位
3 – 8: 打卡日期、时间,格式为“YYMMDDHHMNSS”
9:      进出标志,0为无标志,1为进,2为出。
*/ //写数据库
cmd.CommandText = strSQL;
cmd.ExecuteNonQuery();

count++; //执行成功,计数器加1
}
catch(Exception e)
{
//throw new Exception(e.Message);
} //读下一行
strCurLine = sr.ReadLine(); }
conn.Close();
conn.Dispose();
sr.Close();
blnHasData = true;

Response.Write("<script>alert('成功保存了" + count + "条记录!');</script>"); }
}
if(!blnHasData)
{
Response.Write("<script>alert('没有读取数据!');</script>");
}
}

解决方案 »

  1.   

    DataTable mydt = new DataTable("myTableName");DataColumn mydc;
    DataRow mydr;string strpath = "";
    string strline;
    string [] aryline;mydc = new DataColumn("name");
    mydt.Columns.Add(mydc);mydc = new DataColumn("tel1");
    mydt.Columns.Add(mydc);mydc = new DataColumn("tel2");
    mydt.Columns.Add(mydc);mydc = new DataColumn("mail");
    mydt.Columns.Add(mydc);System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);strline = mysr.ReadLine();
    while(strline != null && strline.Length > 0)
    {
        aryline = strline.Split(new char[]{','});
        mydr = mydt.NewRow();
        mydr[0] = aryline[0];
        mydr[1] = aryline[1];
        mydr[2] = aryline[2];
        mydr[3] = aryline[3];    mydt.Rows.Add(mydr);
        strline = mysr.ReadLine();
    }
      

  2.   

    问一下flygoldfish(长江支流) 
    你using了哪些?
    txtFileContent从何而来?
      

  3.   

    int  intColCount = 0;
    bool blnFlag = true;
    DataTable mydt = new DataTable("myTableName");DataColumn mydc;
    DataRow mydr;string strpath = "";
    string strline;
    string [] aryline;            System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);strline = mysr.ReadLine();
    while(strline != null && strline.Length > 0)
    {               
        aryline = strline.Split(new char[]{'|'});    if (blnFlag)
        {
            blnFlag = false;
            intColCount = aryline.Length;
            for (int i = 0; i < aryline.Length; i++)
            {
                mydc = new DataColumn(aryline[i]);
                mydt.Columns.Add(mydc);
            }                    
        }                    mydr = mydt.NewRow();
        for (int i = 0; i < intColCount; i++)
        {
            mydr[i] = aryline[i];
            mydt.Rows.Add(mydr);
        }
        
        strline = mysr.ReadLine();
    }
      

  4.   

    代码出错:
    for (int i = 0; i < intColCount; i++)
    {
    mydr[i] = aryline[i];
    mydt.Rows.Add(mydr);
    }
    运行,循环到i=1,插入mydt的时候,报该行已经属于此表。
      

  5.   

    楼主,不好意思,mydt.Rows.Add(mydr);应该放到for之外。int  intColCount = 0;
    bool blnFlag = true;
    DataTable mydt = new DataTable("myTableName");DataColumn mydc;
    DataRow mydr;string strpath = "";
    string strline;
    string [] aryline;            System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);while((strline = mysr.ReadLine()) != null)
    {               
        aryline = strline.Split(new char[]{'|'});    if (blnFlag)
        {
            blnFlag = false;
            intColCount = aryline.Length;
            for (int i = 0; i < aryline.Length; i++)
            {
                mydc = new DataColumn(aryline[i]);
                mydt.Columns.Add(mydc);
            }                    
        }                    mydr = mydt.NewRow();
        for (int i = 0; i < intColCount; i++)
        {
            mydr[i] = aryline[i];        
        }
        mydt.Rows.Add(mydr);
    }