标题:[求助]excel中的数据怎样传到SQL里
只看楼主
suntao163
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2006-8-19
 问题点数:0 回复次数:4 
[求助]excel中的数据怎样传到SQL里
    小弟想把excel表中的数据传到SQL数据库中,应该怎么做啊,望高手解答
搜索更多相关主题的帖子: SQL excel 数据库 
2006-09-19 16:03
srufeng
Rank: 1
等 级:新手上路
威 望:2
帖 子:152
专家分:0
注 册:2006-6-5
得分:0 
sql里面有工具可以导入的!就是在选择驱动的时候换一个就行!

2006-09-19 18:32
上炕不脱鞋
Rank: 1
等 级:新手上路
威 望:1
帖 子:332
专家分:0
注 册:2006-3-19
得分:0 

string strPath = File1.PostedFile.FileName;

if(strPath == "")
{
Response.Write("<script>alert('请先选择文件')</script>");
return;
}
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strPath + ";" +
"Extended Properties=Excel 8.0;";

// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);
try
{

// Open connection with the database.
objConn.Open();

// The code to follow uses a SQL SELECT command to display the data from the worksheet.

// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM[sheet1$]", objConn);
/*************************************************************
**********************测试读取单列****************************
OleDbDataReader dr = objCmdSelect.ExecuteReader();
while(dr.Read())
{
Response.Write("产品类别"+dr[2].ToString());
}
dr.Close();
***************************************************************/
// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;

// Create new DataSet to hold information from the worksheet.
DataSet dsolb = new DataSet();

// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(dsolb);

// Bind data to DataGrid control.
DataGrid1.DataSource = dsolb;
DataGrid1.DataBind();
}
catch(Exception ee)
{
Response.Write("<script>alert('请先关闭文件')</script>");
return;
}
finally
{
// Clean up objects.
objConn.Close();
}

//从配置文件中取得数据库连接
string confstring = ConfigurationSettings.AppSettings["SQLConnectionString"].ToString();
string stringzhang = Session["Zhangce"].ToString();
string stringYear = Session["Year2"].ToString();
sqlconn = confstring+stringzhang+"_"+stringYear;

SqlConnection cnn = new SqlConnection(sqlconn);
string sqlcommand="insert into Inventory(cInvCode,cInvName,cInvStd,cInvCCode,dSDate,bsale,bpurchase,bself,bComSume,bProducing,itaxRate,bInvQuality,bInvBatch,bInvEntrust,bInvoverstock,bfree1,bfree2,bInvType,cGroupCode ,cComUnitCode,dModifyDate) "+
"values( @number,@name,@interface,@type,@date,@bsale,@bpurchase,@bself,@bComsume,@bProducing,@itaxRate,@binvquality,@bInvBatch,@binventrust,@binvoverstock,@bfree1,@bfree2,@bInvtype,@cGroupcode,@cComUnitCode,@deModifyDate)";
SqlCommand cmd = new SqlCommand(sqlcommand,cnn);
#region 遍历sql表
cmd.Parameters.Add(new SqlParameter("@type",SqlDbType.VarChar,20));
cmd.Parameters.Add(new SqlParameter("@name",SqlDbType.VarChar,20));
cmd.Parameters.Add(new SqlParameter("@interface",SqlDbType.VarChar,20));
cmd.Parameters.Add(new SqlParameter("@number",SqlDbType.VarChar,20));
cmd.Parameters.Add(new SqlParameter("@date",SqlDbType.SmallDateTime,8,"yyyy-mm-dd"));//设定时间的格式

cmd.Parameters.Add(new SqlParameter("@bsale",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bpurchase",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bself",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bComsume",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bProducing",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@itaxRate",SqlDbType.Real));
cmd.Parameters.Add(new SqlParameter("@binvquality",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bInvBatch",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@binventrust",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@binvoverstock",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bfree1",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bfree2",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@bInvtype",SqlDbType.Bit));
cmd.Parameters.Add(new SqlParameter("@cGroupcode",SqlDbType.VarChar,10));
cmd.Parameters.Add(new SqlParameter("@cComUnitCode",SqlDbType.VarChar,10));
cmd.Parameters.Add(new SqlParameter("@deModifyDate",SqlDbType.DateTime,8,"yyyy-mm-dd"));
#endregion
#region 循环更新到数据库
int numcols = DataGrid1.Items.Count;
for(int i=0;i<=numcols-1;i++)
{
TableCell Name = DataGrid1.Items[i].Cells[0];
TableCell Interface = DataGrid1.Items[i].Cells[1];
TableCell Number = DataGrid1.Items[i].Cells[2];

cmd.Parameters["@type"].Value ="CCPP";

cmd.Parameters["@number"].Value = Number.Text.Trim();
cmd.Parameters["@name"].Value = Name.Text.Trim();
cmd.Parameters["@interface"].Value = Interface.Text.Trim();

cmd.Parameters["@date"].Value = DateTime.Now.ToShortDateString().ToString();

cmd.Parameters["@bsale"].Value = -1;
cmd.Parameters["@bpurchase"].Value = -1;
cmd.Parameters["@bself"].Value = -1;
cmd.Parameters["@bComsume"].Value = -1;

cmd.Parameters["@bProducing"].Value = -1;

cmd.Parameters["@itaxRate"].Value = 17;

cmd.Parameters["@binvquality"].Value = 0;

cmd.Parameters["@bInvBatch"].Value = 0;

cmd.Parameters["@binventrust"].Value = 0;

cmd.Parameters["@binvoverstock"].Value = 0;

cmd.Parameters["@bfree1"].Value = 0;

cmd.Parameters["@bfree2"].Value = 0;

cmd.Parameters["@bInvtype"].Value = 0;

cmd.Parameters["@cGroupcode"].Value = "SL";

cmd.Parameters["@cComUnitCode"].Value = "J";

cmd.Parameters["@deModifyDate"].Value = "2006-11-1";

cnn.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(SqlException ee)
{
Response.Write("<script>alert('错误:导入了相同的表或Excel表头位置错误,请改成产品名称、产品规格、产品编号的顺序!')</script>");
return;
}
finally
{
cnn.Close();
}
#endregion


新建的群(C#):23384106 验证注明
2006-09-21 08:46
上炕不脱鞋
Rank: 1
等 级:新手上路
威 望:1
帖 子:332
专家分:0
注 册:2006-3-19
得分:0 
这是我前些天写得,有点乱,你看看吧

新建的群(C#):23384106 验证注明
2006-09-21 08:46
suntao163
Rank: 1
等 级:新手上路
帖 子:58
专家分:0
注 册:2006-8-19
得分:0 

谢谢啊

2006-10-09 10:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-91295-1-1.html




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

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