當前位置:首頁 » 編程語言 » c從excel導入sql

c從excel導入sql

發布時間: 2022-08-19 13:56:36

① 如何將excel表的內容導入sql資料庫

從Excel文件中,導入數據到SQL資料庫中,很簡單,直接用下面的語句:
/*===================================================================*/
select * from OpenRowSet('microsoft.jet.oledb.4.0','Excel 8.0;HDR=Yes;database=c:\ftng.xls','select * from [FTNG$]')
--如果接受數據導入的表已經存在
insert into 表 select * from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
--如果導入數據並生成表
select * into 表 from
OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
/*===================================================================*/
--如果從SQL資料庫中,導出數據到Excel,如果Excel文件已經存在,而且已經按照要接收的數據創建好表頭,就可以簡單的用:
insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
select * from 表
--如果Excel文件不存在,也可以用BCP來導成類Excel的文件,注意大小寫:
--導出表的情況

EXEC master..xp_cmdshell 'bcp 資料庫名.dbo.表名 out "c:\test.xls" /c -/S"伺服器名" /U"用戶名" -P"密碼"'

② 怎麼寫sql語句將Excel的數據插入到SQL Server資料庫

方法和詳細的操作步驟如下:

1、第一步,准備Excel表,並希望將其導入SQL
Server 2012中的QearlTest資料庫,見下圖,轉到下面的步驟。

③ 怎樣把Excel數據導入SQL資料庫中,該如何處理

怎樣把Excel數據導入SQL資料庫中,該如何處理
方法如下:

1、打開要導入的Excel文件,觀察第一列是為欄位還是數據。

2、打開SQLServer,在需要導入的數據點擊右鍵 【任務】-【導入數據】
出現導入導出向導。

3、點擊下一步 ,進入【選擇數據源】頁面,注意紅框設置。

4、點擊下一步 ,進入【選擇目標】頁面,注意紅框設置。

5、點擊下一步 ,進入【指定表復制或查詢】頁面,注意紅框設置。

6、點擊下一步 ,進入【選擇源表和源視圖】頁面,注意紅框設置。

7、下一步,直到完成。出現【執行結果頁面】。

8、最後在SqlServer查詢表。

④ C#中如何將Excel中的數據批量導入到sql server

1.本文實現在c#中可高效的將excel數據導入到sqlserver資料庫中,很多人通過循環來拼接sql,這樣做不但容易出錯而且效率低下,最好的辦法是使用bcp,也就是System.Data.SqlClient.SqlBulkCopy 類來實現。不但速度快,而且代碼簡單,下面測試代碼導入一個6萬多條數據的sheet,包括讀取(全部讀取比較慢)在我的開發環境中只需要10秒左右,而真正的導入過程只需要4.5秒。
2.代碼如下:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//測試,將excel中的sheet1導入到sqlserver中
string connString = "server=localhost;uid=sa;pwd=sqlgis;database=master";
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
TransferData(fd.FileName, "sheet1", connString);
}
}

public void TransferData(string excelFile, string sheetName, string connectionString)
{
DataSet ds = new DataSet();
try
{
//獲取全部數據
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]", sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds, sheetName);

//如果目標表不存在則創建
string strSql = string.Format("if object_id('{0}') is null create table {0}(", sheetName);
foreach (System.Data.DataColumn c in ds.Tables[0].Columns)
{
strSql += string.Format("[{0}] varchar(255),", c.ColumnName);
}
strSql = strSql.Trim(',') + ")";

using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString))
{
sqlconn.Open();
System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();
command.CommandText = strSql;
command.ExecuteNonQuery();
sqlconn.Close();
}
//用bcp導入數據
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString))
{
bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;//每次傳輸的行數
bcp.NotifyAfter = 100;//進度提示的行數
bcp.DestinationTableName = sheetName;//目標表
bcp.WriteToServer(ds.Tables[0]);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}

//進度顯示
void bcp_SqlRowsCopied(object sender, System.Data.SqlClient.SqlRowsCopiedEventArgs e)
{
this.Text = e.RowsCopied.ToString();
this.Update();
}
}
}
3.上面的TransferData基本可以直接使用,如果要考慮周全的話,可以用oledb來獲取excel的表結構,並且加入ColumnMappings來設置對照欄位,這樣效果就完全可以做到和sqlserver的dts相同的效果了。

⑤ 使用C# 把Excel的數據導入SQL server 2008

由於項目中加入了新的功能,可以使管理員向資料庫中導入Excel數據。因此,在商品管理這塊需要對Excel進行操作,在網上查了些資料,根據項目的實際情況進行了一定的優化,這里簡單的介紹下。

1.C#代碼。

<spanstyle="font-family:MicrosoftYaHei;font-size:18px;">///<summary>

///上傳Excel文件,並將數據導入到資料庫

///</summary>

///<paramname="sender"></param>

///<paramname="e"></param>

protectedvoidlbtnSure_Click(objectsender,EventArgse)

{

//定義變數,並賦初值

stringurl=this.fileUpLoad.PostedFile.FileName;

stringurlLocation="";

//判斷傳輸地址是否為空

if(url=="")

{

//提示「請選擇Excel文件」

Page.ClientScript.RegisterStartupScript(Page.GetType(),"message","<scriptdefer>alert('請選擇97~2003版Excel文件!');</script>");

return;

}

//判斷獲取的是否為地址,而非文件名

if(url.IndexOf("\")>-1)

{

//獲取文件名

urlLocation=url.Substring(url.LastIndexOf("\")+1);//獲取文件名

}

else

{

//url為文件名時,直接獲取文件名

urlLocation=url;

}

//判斷指定目錄下是否存在文件夾,如果不存在,則創建

if(!Directory.Exists(Server.MapPath("~\up")))

{

//創建up文件夾

Directory.CreateDirectory(Server.MapPath("~\up"));

}

//在系統中建文件夾up,並將excel文件另存

this.fileUpLoad.SaveAs(Server.MapPath("~\up")+"\"+urlLocation);//記錄文件名到伺服器相對應的文件夾中

//Response.Write(urlLocation);

//取得保存到伺服器端的文件路徑

stringstrpath=Server.MapPath("~\up")+"\"+urlLocation;

//取得config中的欄位

stringconnectionString=ConfigurationManager.AppSettings["Connect"].ToString();

stringstrCon=ConfigurationManager.AppSettings["strUpLoad"].ToString();

//替換變數

strCon=strCon.Replace("$Con$",strpath);

//初始化導入Excel對象

ImportExcelexcel=newImportExcel();

//調用方法,將Excel文件導入資料庫

excel.TransferData(strCon,"t_Goods",connectionString);

}</span>

2.TransferData類。

<spanstyle="font-family:MicrosoftYaHei;font-size:18px;">publicvoidTransferData(stringstrCon,stringsheetName,stringconnectionString)

{

DataSetds=newDataSet();

try

{

//獲取全部數據

OleDbConnectionconn=newOleDbConnection(strCon);

conn.Open();

stringstrExcel="";

OleDbDataAdaptermyCommand=null;

strExcel=string.Format("select*from[{0}$]",sheetName);

myCommand=newOleDbDataAdapter(strExcel,strConn);

myCommand.Fill(ds,sheetName);

//如果目標表不存在則創建,excel文件的第一行為列標題,從第二行開始全部都是數據記錄

stringstrSql=string.Format("ifnotexists(select*fromsysobjectswherename='{0}')createtable{0}(",sheetName);//以sheetName為表名

foreach(System.Data.DataColumncinds.Tables[0].Columns)

{

strSql+=string.Format("[{0}]varchar(255),",c.ColumnName);

}

strSql=strSql.Trim(',')+")";

using(System.Data.SqlClient.SqlConnectionsqlconn=newSystem.Data.SqlClient.SqlConnection(connectionString))

{

sqlconn.Open();

System.Data.SqlClient.SqlCommandcommand=sqlconn.CreateCommand();

command.CommandText=strSql;

command.ExecuteNonQuery();

sqlconn.Close();

}

//用bcp導入數據

//excel文件中列的順序必須和數據表的列順序一致,因為數據導入時,是從excel文件的第二行數據開始,不管數據表的結構是什麼樣的,反正就是第一列的數據會插入到數據表的第一列欄位中,第二列的數據插入到數據表的第二列欄位中,以此類推,它本身不會去判斷要插入的數據是對應數據表中哪一個欄位的

using(System.Data.SqlClient.SqlBulkCopybcp=newSystem.Data.SqlClient.SqlBulkCopy(connectionString))

{

bcp.SqlRowsCopied+=newSystem.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);

bcp.BatchSize=100;//每次傳輸的行數

bcp.NotifyAfter=100;//進度提示的行數

bcp.DestinationTableName=sheetName;//目標表

bcp.WriteToServer(ds.Tables[0]);

}

}

catch(Exceptionex)

{

thrownewException(ex);

}

}

//進度顯示

voidbcp_SqlRowsCopied(objectsender,System.Data.SqlClient.SqlRowsCopiedEventArgse)

{

}

}</span>

4.Web界面樣式。

⑥ 怎麼把excel文件里的數據導入SQL資料庫

具體操作步驟如下:

1、首先雙擊打開sqlserver,右擊需要導入數據的資料庫,如圖所示。

⑦ 怎樣將EXCEL數據表導入到SQL中

第一步:登錄到
sql
server
management
studio
第二步:在
「對象資源管理器
」中右鍵單擊
「管理
」,在彈出列表中單擊
「導入數據

第三步:在
「導入向導
」對話框中單擊
「下一步
」,進入到
「選擇數據源
」對話框,在
「數據源
」列表中選擇
「microsoft
excel
」,同時選擇相應的
excel
文檔,完成後單擊
「下一步
」(一定要勾選該對話框中的
「首行包含列名稱
」,因此它是將
excel文檔中的列標題為資料庫表中的列項標題)
第四步:指定目標資料庫服務,依次單擊
「下一步
」。。。。至到
「完成

第五步:重新打到
sql
server
management
studio,進入到導入的資料庫表,可以發現所導入的
excel文檔數據。

⑧ C# 怎麼把一個excel導入到SQL裡面去

將Excel的數據導入SQL server :
SELECT * into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\book1.xls";User ID=Admin;Password=;
Extended properties=Excel 5.0')...[Sheet1$]
實例:
SELECT * into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;
Extended properties=Excel 5.0')...xactions

熱點內容
株洲移動網路伺服器地址 發布:2024-04-20 07:45:48 瀏覽:689
119濃度的鹽酸怎麼配置 發布:2024-04-20 06:23:38 瀏覽:119
資料庫pf 發布:2024-04-20 05:57:36 瀏覽:393
c語言編譯出現連接錯誤 發布:2024-04-20 05:42:18 瀏覽:198
湖北戴爾伺服器維修系統雲主機 發布:2024-04-20 05:36:47 瀏覽:62
android上傳數據 發布:2024-04-20 05:36:43 瀏覽:143
python替換文本內容 發布:2024-04-20 05:21:22 瀏覽:899
urllib3源碼 發布:2024-04-20 05:11:23 瀏覽:35
如何通過運行打開伺服器文件 發布:2024-04-20 00:06:50 瀏覽:671
電腦百度網盤怎麼上傳 發布:2024-04-19 23:49:15 瀏覽:417