方法1:
1、打开企业管理器,打开要导入数据的数据库,在表上按右键,所有任务-->导入数据,弹出DTS导入/导出向导,按 下一步 ,
2、选择数据源 Microsoft Excel 97-2000,文件名 选择要导入的xls文件,按 下一步 ,
3、选择目的 用于SQL Server 的Microsoft OLE DB提供程序,服务器选择本地(如果是本地数据库的话,如 VVV),使用SQL Server身份验证,用户名sa,密码为空,数据库选择要导入数据的数据库(如 client),按 下一步 ,
4、选择 用一条查询指定要传输的数据,按 下一步 ,
5、按 查询生成器,在源表列表中,有要导入的xls文件的列,将各列加入到右边的 选中的列 列表中,这一步一定要注意,加入列的顺序一定要与数据库中字段定义的顺序相同,否则将会出错,按 下一步 ,
6、选择要对数据进行排列的顺序,在这一步中选择的列就是在查询语句中 order by 后面所跟的列,按 下一步 ,
7、如果要全部导入,则选择 全部行,按 下一步,
8、则会看到根据前面的操作生成的查询语句,确认无误后,按 下一步,
9、会看到 表/工作表/Excel命名区域 列表,在 目的 列,选择要导入数据的那个表,按 下一步,
10、选择 立即运行,按 下一步,
11、会看到整个操作的摘要,按 完成 即可。
方法2:
private void btnOK_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Server=(local);Initial Catalog=Northwind;uid=sa;pwd=111;");
myConnection.Open();
// 启动一个事务
SqlTransaction myTrans = myConnection.BeginTransaction();
// 为事务创建一个命令
SqlCommand myCommand = new SqlCommand();
myCommand.Connection=myConnection;
myCommand.Transaction = myTrans;
try
{
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (110, 'Description')";
myCommand.ExecuteNonQuery();
//myTrans.Commit();
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (111, 'Description')";
myCommand.ExecuteNonQuery();
myTrans.Commit();
Response.Write("成功写入记录!");
}
catch(Exception Ex)
{
myTrans.Rollback();
Response.Write(Ex.ToString());
Response.Write("写入数据库失败!");
}
finally
{
myConnection.Close();
}
}
--------------------------上面是网上的最基本的回gun操作,但无验证 和 循环中的回gun end
-------------------------------我调啊,调,唉,好不容易搞出个乱程序,水平太挫了,唉,怕下欠忘记,放上来吧..-----------------
----------------- --------------------------------------------------------------------
protected void Button1_Click(object sender, EventArgs e)
{
if (File1.PostedFile != null)
{
Random rd = new Random(1);
string filename = DateTime.Now.Date.ToString("yyyyMMdd") + DateTime.Now.ToLongTimeString().Replace(":", "") + rd.Next(9999).ToString() + ".xls";
string path = Server.MapPath( Request.ApplicationPath) + @"/upload"; //路径取得
File1.PostedFile.SaveAs(path + "//" + filename);
Label1.Text = "鏂囦欢鍚嶄负" + filename;
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + path + "//" + filename + ";Extended Properties=Excel 8.0";
OleDbConnection thisconnection = new OleDbConnection(conn);
thisconnection.Open();
string Sql = "select * from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
DataSet ds = new DataSet();
mycommand.Fill(ds, "[Sheet1$]");
thisconnection.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataBind(); ////////////////////////////放入ds
string conn1 = "Data Source=rainingheart;Initial Catalog=rainingheartDB;User ID=sa;Password=rainingheartp@ssw0d;Provider=SQLOLEDB.1;";
OleDbConnection thisconnection1 = new OleDbConnection(conn1);
thisconnection1.Open();
OleDbTransaction trans = thisconnection1.BeginTransaction();
OleDbCommand mycommand1 = new OleDbCommand();
mycommand1.Connection = thisconnection1;
mycommand1.Transaction = trans;
int count = ds.Tables["[Sheet1$]"].Rows.Count;
for (int i = 0; i < count; i++)
{
string strBargainID, strSecurity, strSanitation, strFirecontrol, strOrder, strDeal, strPeccancy;
//Data Info Get
strBargainID = ds.Tables["[Sheet1$]"].Rows[i]["clientID"].ToString();
strSecurity = ds.Tables["[Sheet1$]"].Rows[i]["Security"].ToString();
strSanitation = ds.Tables["[Sheet1$]"].Rows[i]["Sanitation"].ToString();
strFirecontrol = ds.Tables["[Sheet1$]"].Rows[i]["Firecontrol"].ToString();
strOrder = ds.Tables["[Sheet1$]"].Rows[i]["Order"].ToString();
strDeal = ds.Tables["[Sheet1$]"].Rows[i]["Deal"].ToString();
strPeccancy = ds.Tables["[Sheet1$]"].Rows[i]["Peccancy"].ToString();
//ValidCheck(); ' forbid
if (funHelp.ContainSingleQuote(strBargainID))
{
trans.Rollback();
ClientScript.RegisterStartupScript(this.GetType(), "failCheck", funHelp.getScript("绗? + (i+1).ToString() + "" + "绗 竴鍒椾笉鑳芥湁鍗曞紩鍙?鏁版嵁鏇存柊鎿嶄綔宸插彇娑?"));
return;
}
string strCmd = "";
//////////////////////////// 唉这段把我自己搞的晕晕的,虽然跑通了,但为什么还不理解 ---start
OleDbCommand mycommand2 = new OleDbCommand();
mycommand2.Connection = thisconnection1;
mycommand2.Transaction = trans;
mycommand2.CommandText = "select 1 from tblviolation where clientid = '" + strBargainID + "'";
OleDbDataReader dr = mycommand2.ExecuteReader();
//////////////////////////// 唉这段把我自己搞的晕晕的,虽然跑通了,但为什么还不理解 --END
// if exists ,update else insert
if (dr.Read())
{
strCmd = "update tblviolation set [security] =' " + funHelp.SqlSingleQuote(strSecurity) +
"',sanitation = ' " + funHelp.SqlSingleQuote(strSanitation) + "',firecontrol = '" +
funHelp.SqlSingleQuote(strFirecontrol) + "',[order] = '" + funHelp.SqlSingleQuote(strOrder) + "',deal='" +
funHelp.SqlSingleQuote(strDeal) + "',Peccancy ='" + funHelp.SqlSingleQuote(strPeccancy) + "'" +
" where clientID = '" + strBargainID + "'";
}
else
{
strCmd = "insert into tblviolation (clientid,[security],sanitation,firecontrol,[order],deal,Peccancy) " +
" values('" + funHelp.SqlSingleQuote(strBargainID) + "',' " + funHelp.SqlSingleQuote(strSecurity) +
"','" + funHelp.SqlSingleQuote(strSanitation) + "',' " + funHelp.SqlSingleQuote(strFirecontrol) +
"','" + funHelp.SqlSingleQuote(strOrder) + "','" + funHelp.SqlSingleQuote(strDeal) + "','" +
funHelp.SqlSingleQuote(strPeccancy) + "')";
}
mycommand1.CommandText = strCmd;
mycommand1.ExecuteNonQuery();
}
trans.Commit();
Response.Write("鏇存柊鎴愬姛");
thisconnection1.Close();
}
}
//////////////////////////////再加了个try catch
protected void Button1_Click(object sender, EventArgs e)
{
if (File1.PostedFile != null)
{
Random rd = new Random(1);
string filename = DateTime.Now.Date.ToString("yyyyMMdd") + DateTime.Now.ToLongTimeString().Replace(":", "") + rd.Next(9999).ToString() + ".xls";
string path = Server.MapPath(Request.ApplicationPath) + @"/upload";
File1.PostedFile.SaveAs(path + "//" + filename);
Label1.Text = "鏂囦欢鍚嶄负" + filename;
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + path + "//" + filename + ";Extended Properties=Excel 8.0";
OleDbConnection thisconnection = new OleDbConnection(conn);
try
{
thisconnection.Open();
}
catch
{
ClientScript.RegisterStartupScript(this.GetType(), "failOpen", funHelp.getScript("瀵煎叆鏂囦欢鏍煎紡涓嶆 纭 垨鑰呮枃浠秙heet鍚嶉敊璇 紝鏁版嵁鏇存柊鎿嶄綔宸插彇娑?"));
thisconnection.Close();
return;
}
string Sql = "select * from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
DataSet ds = new DataSet();
mycommand.Fill(ds, "[Sheet1$]");
thisconnection.Close();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
string conn1 = "Data Source=rainingheart;Initial Catalog=rainingheartDB;User ID=sa;Password=rainingheartp@ssw0d;Provider=SQLOLEDB.1;";
OleDbConnection thisconnection1 = new OleDbConnection(conn1);
thisconnection1.Open();
//try
//{
// thisconnection1.Open();
//}
//catch
//{
// ClientScript.RegisterStartupScript(this.GetType(), "failOpen", funHelp.AlertScript("瀵煎叆鏂囦欢鏍煎紡涓嶆 纭 垨鑰呮枃浠秙heet鍚嶉敊璇 紝鏁版嵁鏇存柊鎿嶄綔宸插彇娑?"));
// return;
//}
OleDbTransaction trans = thisconnection1.BeginTransaction();
OleDbCommand mycommand1 = new OleDbCommand();
mycommand1.Connection = thisconnection1;
mycommand1.Transaction = trans;
try
{
int count = ds.Tables["[Sheet1$]"].Rows.Count;
for (int i = 0; i < count; i++)
{
string strBargainID, strSecurity, strSanitation, strFirecontrol, strOrder, strDeal, strPeccancy;
//Data Info Get
strBargainID = ds.Tables["[Sheet1$]"].Rows[i]["clientID"].ToString();
strSecurity = ds.Tables["[Sheet1$]"].Rows[i]["Security"].ToString();
strSanitation = ds.Tables["[Sheet1$]"].Rows[i]["Sanitation"].ToString();
strFirecontrol = ds.Tables["[Sheet1$]"].Rows[i]["Firecontrol"].ToString();
strOrder = ds.Tables["[Sheet1$]"].Rows[i]["Order"].ToString();
strDeal = ds.Tables["[Sheet1$]"].Rows[i]["Deal"].ToString();
strPeccancy = ds.Tables["[Sheet1$]"].Rows[i]["Peccancy"].ToString();
//ValidCheck();
if (funHelp.ContainSingleQuote(strBargainID))
{
trans.Rollback();
ClientScript.RegisterStartupScript(this.GetType(), "failCheck", funHelp.AlertScript("绗? + (i + 1).ToString() + "" + "绗 竴鍒椾笉鑳芥湁鍗曞紩鍙?鏁版嵁鏇存柊鎿嶄綔宸插彇娑?"));
return;
}
string strCmd = "";
OleDbCommand mycommand2 = new OleDbCommand();
mycommand2.Connection = thisconnection1;
mycommand2.Transaction = trans;
mycommand2.CommandText = "select 1 from tblviolation where clientid = '" + strBargainID + "'";
OleDbDataReader dr = mycommand2.ExecuteReader();
if (dr.Read())
{
strCmd = "update tblviolation set [security] =' " + funHelp.SqlSingleQuote(strSecurity) +
"',sanitation = ' " + funHelp.SqlSingleQuote(strSanitation) + "',firecontrol = '" +
funHelp.SqlSingleQuote(strFirecontrol) + "',[order] = '" + funHelp.SqlSingleQuote(strOrder) + "',deal='" +
funHelp.SqlSingleQuote(strDeal) + "',Peccancy ='" + funHelp.SqlSingleQuote(strPeccancy) + "'" +
" where clientID = '" + strBargainID + "'";
}
else
{
strCmd = "insert into tblviolation (clientid,[security],sanitation,firecontrol,[order],deal,Peccancy) " +
" values('" + funHelp.SqlSingleQuote(strBargainID) + "',' " + funHelp.SqlSingleQuote(strSecurity) +
"','" + funHelp.SqlSingleQuote(strSanitation) + "',' " + funHelp.SqlSingleQuote(strFirecontrol) +
"','" + funHelp.SqlSingleQuote(strOrder) + "','" + funHelp.SqlSingleQuote(strDeal) + "','" +
funHelp.SqlSingleQuote(strPeccancy) + "')";
}
mycommand1.CommandText = strCmd;
mycommand1.ExecuteNonQuery();
}//end for
trans.Commit();
Response.Write("鏇存柊鎴愬姛");
thisconnection1.Close();
}
catch
{
trans.Rollback();
ClientScript.RegisterStartupScript(this.GetType(), "failCheck", funHelp.getScript("鏁版嵁鏇存柊鏈夊紓甯告搷浣滃凡鍙栨秷!"));
return;
}
}// if
}//end fun
方法3:
在网上找到一个Asp.Net将Excel数据导入Sql Server数据库的的例子,是调用存储过程的,大家分享. 我刚用过,感觉还可以。其基本思想是先新建一个页面,放一个HTML控件 File FIled 控件,然后将这个控件转化为服务器控件, 再放一个按钮(服务器控件)用来获取File FIled中的内容,接下来就是在后台编程了,首先引入两个命名空间,using System.data.oledb和using system.data.sqlclint 其中oledb空间用来提供Excel的驱动,sqlclint用来连接sql server 2000用,接下来就可以在按钮事件中把下面的代码搞上去就行了,对了还要编写自己的存储过程哦! 前言:cmd.CommandText="Proc_Address";这句话是调用存储过程"Proc_Address" |