| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1136 人关注过本帖
标题:往SQL中导入EXCEL数据?
只看楼主 加入收藏
swpihchj
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2006-3-31
收藏
 问题点数:0 回复次数:7 
往SQL中导入EXCEL数据?
往SQL中导入EXCEL数据时,若要创建的表已经存在,任何把数据追加在表记录的后面?
帮帮忙!
搜索更多相关主题的帖子: SQL EXCEL 数据 
2006-05-16 20:20
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
C# 里面

select到dataset

读入一行

加row,写入row,直到完,

然后整个更新


你要代码的话,我明天回公司在传上来,我现在在家……
2006-05-16 20:45
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

try
{
OleDbConnection oleDbConnection = new OleDbConnection(connExcel);
oleDbConnection.Open();

dataSet = new DataSet();


DataTable dataTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); //获取excel表


string tableName1 = dataTable.Rows[0][2].ToString().Trim(); //获取Sheet1名
//[0][1]...[N]: 按名称排列的表单元素

tableName1 = "[" + tableName1.Replace("'","") + "]";

//哩两句是用来获取课程信息用的
string query1 = "SELECT * FROM " + tableName1;

OleDbDataAdapter oleAdapter1 = new OleDbDataAdapter(query1,connExcel);
oleAdapter1.Fill(dataSet,"gch_CustomerCard_Info");

dataGrid4.SetDataBinding(dataSet,"gch_CustomerCard_Info");


oleDbConnection.Close();

DataTable dataTable1 = new DataTable("gch_Customer_Info");

string getCustomerInfo = @"select customerID, bargainID, company, address, contactWay, zipcode,
linkman, linkmanBirthday, linkmanSex, linkmanPosition, linkmanFax,
linkmanPhone, linkmanMobile, linkmanEmail, principal, principalBirthday,
principalSex, principalPosition, principalFax, principalPhone, principalMobile,
principalEmail, companyProperty, companyType, companySetUpDate, businessMode,
companySize, nowService, serviceLocation, adviser, adviserLocation, adminID, recordDate

from gch_Customer_Info";

SqlDataAdapter sqlDA1 = new SqlDataAdapter(getCustomerInfo,sqlConnection1);

SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1);

sqlDA1.Fill(dataTable1);

//int count = dataSet.Tables["gch_CustomerCard_Info"].Columns.Count;
//count = count - 1;

///*

foreach(DataRow dataRow in dataSet.Tables["gch_CustomerCard_Info"].Rows)
{
//for(int i=0; i<count; i++)
//{
DataRow dataRow1 = dataTable1.NewRow();
//MessageBox.Show(dataRow[i+1].ToString());

//if(dataRow[i+1].ToString()!="")
{
if(dataRow["权益书号"].ToString()!="")
{
dataRow1["bargainID"] = dataRow["权益书号"];
}
else
{
dataRow1["bargainID"] = "0";
}

dataRow1["company"] = dataRow["公司名称"];

dataRow1["address"] = dataRow["通讯地址"];

dataRow1["contactWay"] = dataRow["通知方式"];

dataRow1["zipcode"] = dataRow["邮编"];

dataRow1["linkman"] = dataRow["联系人"];

dataRow1["linkmanBirthday"] = "1983-3-1"; //dataRow["联系人生日"];


//dataRow1["linkmanSex"] = dataRow["性别"];
switch(dataRow["性别"].ToString())
{
case "男":
dataRow1["linkmanSex"] = 1;
break;
case "女":
dataRow1["linkmanSex"] = 0;
break;
default:
dataRow1["linkmanSex"] = DBNull.Value;
break;
}

dataRow1["linkmanPosition"] = dataRow["职位"];

dataRow1["linkmanFax"] = dataRow["传真"];

dataRow1["linkmanPhone"] = dataRow["电话"];

dataRow1["linkmanMobile"] = dataRow["手机"];

dataRow1["linkmanEmail"] = dataRow["email"];

dataRow1["principal"] = dataRow["公司负责人"];

dataRow1["principalBirthday"] = "1985-7-5"; //dataRow["负责人生日"];


//dataRow1["principalSex"] = dataRow["性别1"];
switch(dataRow["性别1"].ToString())
{
case "男":
dataRow1["principalSex"] = 1;
break;
case "女":
dataRow1["principalSex"] = 0;
break;
default:
dataRow1["principalSex"] = DBNull.Value;
break;
}

dataRow1["principalPosition"] = dataRow["职位1"];

dataRow1["principalFax"] = dataRow["传真1"];

dataRow1["principalPhone"] = dataRow["电话1"];

dataRow1["principalMobile"] = dataRow["手机1"];

dataRow1["principalEmail"] = dataRow["email1"];

dataRow1["companyProperty"] = dataRow["企业性质"];

dataRow1["companyType"] = dataRow["行业类型"];

dataRow1["companySetUpDate"] = "1983-10-2"; //dataRow["成立日期"];

dataRow1["businessMode"] = dataRow["经营模式"];

dataRow1["companySize"] = dataRow["企业规模"];

dataRow1["nowService"] = dataRow["现服务人"];

dataRow1["serviceLocation"] = dataRow["区域"];

dataRow1["adviser"] = dataRow["顾问"];

dataRow1["adviserLocation"] = dataRow["分公司"];
}

//if(dataRow1[1].ToString()!="") //&& dataRow1[2].ToString()!="" && dataRow1[3].ToString()!="" && dataRow1[4].ToString()!="")
//{
dataTable1.Rows.Add(dataRow1);
//}
//}
}

//sqlDA1.Update(dataTable1); //哩句系插入数据库的关键

MessageBox.Show("新插入 " + dataTable1.Rows.Count.ToString() + " 条记录");

DataSet ds = new DataSet();

ds.Tables.Add(dataTable1);

dataGrid5.SetDataBinding(ds,"gch_Customer_Info");
//*/

}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

[此贴子已经被作者于2006-5-17 10:31:46编辑过]

2006-05-17 10:31
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

... //前一段先读出excel文件里面的内容


SqlDataAdapter sqlDA1 = new SqlDataAdapter(getCustomerInfo,sqlConnection1);
SqlCommandBuilder sqlCB1 = new SqlCommandBuilder(sqlDA1);
sqlDA1.Fill(dataTable1);
//上面三句先把原来的数据读出来


//设置一个循环 每次循环都新建一行
{
DataRow dataRow1 = dataTable1.NewRow();


... //一行一行地把excel里面的数据插入datatable


dataTable1.Rows.Add(dataRow1);
}//循环完毕

sqlDA1.Update(dataTable1); //插入数据库的关键

2006-05-17 10:46
swpihchj
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2006-3-31
收藏
得分:0 
太感谢了!
2006-05-17 18:20
swpihchj
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2006-3-31
收藏
得分:0 
我说的是在VB.NET中啊!
2006-05-17 18:30
live4what
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-5-17
收藏
得分:0 

I want to grow up! I\'m serious!
2006-05-18 13:50
live4what
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-5-17
收藏
得分:0 
是通用的

I want to grow up! I\'m serious!
2006-05-18 13:59
快速回复:往SQL中导入EXCEL数据?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052804 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved