我有个excel的表格,数据如下:
name age address
aaa 21 东莞
bbb 20 广州
ccc 18 深圳
ddd 23 阳江
我把那个Excel表格储存在"mydata.csv"文件中,把他存储在"mydata.xls"中时,读出来放在数据库里倒是没问题,可是一变成CSV格式后,储存到数据库中,我的中文却不见了,第一条记录的address变成了"[]"了,而其他的地址那栏却成了""了.应该是乱码的问题吧,我想,但是不知怎么样解决.哪位知道的请告诉小弟一下,谢谢了.
下面是代码:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(ConnectionString);
//try
//{
using (StreamReader sr = new StreamReader(Server.MapPath("mydata.csv")))
{
//GridView1.DataSource = sr;
//GridView1.DataBind();
String line;
SqlDataAdapter sqlDA1 = new SqlDataAdapter("SELECT * FROM tbTest", myConnection);//tbTest 是数据库中的表.也就是将EXCEL数据添加的那张表.
DataTable dataTable1 = new DataTable();
SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1);
sqlDA1.Fill(dataTable1);
DataRow dataRow1 = null;
// Read and display lines from the file until the end of
// the file is reached.
StreamReader sr2 = new StreamReader(Server.MapPath("mydata.csv"));
sr2.ReadLine();
while (sr.ReadLine()!=null)
{
line=sr2.ReadLine();
string[] value = line.Split(',');
dataRow1 = dataTable1.NewRow();
dataRow1["name"] = value[0]; //以下的名字全是数据库的列名
dataRow1["age"] = Convert.ToInt32 (value[1]);
dataRow1["address"] = value[2];
dataTable1.Rows.Add(dataRow1);
}
Response.Write("新插入 " + dataTable1.Rows.Count.ToString() + " 条记录");
sqlDA1.Update(dataTable1);
}
}
catch (Exception exp)
{
Response .Write ("The file could not be read:");
Response.Write(exp.Message);
}
}
上面的非中文却可以写进去,中文就不行了,请高手指教