Home > Net >  Questions about the file import SQL server database, senior counsel, please. Thank you
Questions about the file import SQL server database, senior counsel, please. Thank you

Time:09-24

Find a code for several days and Microsoft help, still can't find want processing method, using BULK insert import file is all direct import and must be determined format and table format corresponding
I want import column according to the requirement of data, I see OPENROWSET, but did not understand, I do not know whether accord with I need (show just a little trouble understanding people thank you)
Want to write a winfrom program, the selected file path, set up import column is the file which columns, can not choose not to import, more sets of data (such as file with three group id in one line, the name (name1 id1, id2, name2, id3, name3) data table only id, the name that I need to set up into three groups,)
Like the image below,
Find the following code, but I am stupid not understand , go to https://www.cnblogs.com/superlee/archive/2010/01/12/1645292.html
The code below
 string fPath=this. FName. PostedFile. The FileName;//get to import a text file 
String extName=fPath. Substring (fPath LastIndexOf (". ") + 1);//get file extensions
SqlConnection con=new SqlConnection (" server=. The database=pubs; Uid=sa; The PWD=;" );//database connection object
Con. The Open ();
String mystring="Provider=Microsoft. Jet. The OLEDB. 4.0; The Data Source='" + fPath + "'; Extended Properties=8.0 "Excel;
OleDbConnection CNNXLS=new OleDbConnection (mystring);
OleDbDataAdapter myDa=new OleDbDataAdapter (" select * from [Sheet1 $] ", CNNXLS);
The DataSet myDs=new DataSet ();
MyDa. The Fill (myDs);
If (myDs. Tables [0]. Rows. Count & gt; 0)
{
String strSql="";
String CnnString="Provider=SQLOLEDB; The database=pubs; Server=. Uid=sa; The PWD=";
OleDbConnection conn=new OleDbConnection (CnnString);
Conn. The Open ();
OleDbCommand myCmd=null;
for(int i=0; i{
StrSql="insert into txtInsert (id, name) values (";
StrSql +=myDs. Tables [0]. Rows [I] ItemArray [0]. The ToString () + ", "";
StrSql +=myDs. Tables [0]. Rows [I] ItemArray [1]. The ToString () + "");"
MyCmd=new OleDbCommand (strSql, conn);
Try
{
MyCmd. ExecuteNonQuery ();
Response. Write (" & lt; Script language=javascript& gt; Alert (' data import is successful! ') & lt;/script>" );
}
The catch (OleDbException err)
{
Response. Write (" error importing the database: "+ err. ToString ());
break;
}
}
Conn. Close ();


If you have any elder passing by, please tell, grateful

CodePudding user response:

Above written by Delphi, no source, so can't

CodePudding user response:


The problem of import, and in the process you use ADO.NET nothing difference between

Does you read the data in the DataTable, then Insert into the database, ADO.NET usage is the same

Is don't want to say anything, you need to read the data from the file in
Don't know what you from file to import, Excel? TXT?

CodePudding user response:

refer to the second floor Eason0807 response:
import issue, and in the process you use ADO.NET nothing difference between

Does you read the data in the DataTable, then Insert into the database, ADO.NET usage is the same

Is don't want to say anything, you need to read the data from the file in
Don't know what you from file to import, Excel? TXT?


At present is to want to from TXT import, can you tell me how to use ADO.net? If know want to write judgment to choose, thank you

CodePudding user response:

refer to the second floor Eason0807 response:
import issue, and in the process you use ADO.NET nothing difference between

Does you read the data in the DataTable, then Insert into the database, ADO.NET usage is the same

Is don't want to say anything, you need to read the data from the file in
Don't know what you from file to import, Excel? TXT?


You have said is directly while testing, and then use the insert directly into a a data import is? So how do I select text columns?

CodePudding user response:

reference 4 floor qq_41654492 response:
Quote: refer to the second floor Eason0807 response:


The problem of import, and in the process you use ADO.NET nothing difference between

Does you read the data in the DataTable, then Insert into the database, ADO.NET usage is the same

Is don't want to say anything, you need to read the data from the file in
Don't know what you from file to import, Excel? TXT?


You have said is directly while testing, and then use the insert directly into a a data import is? So how do I select text columns?


Once a cycle to the conventional way, don't suggest you do that, every time to open a connection very consume resources;
Common practice when spelling a SQL statement, but if the data quantity is big, not a whole put together, to segment;
If you use some O/RM framework, the process can be very simple

CodePudding user response:

reference 5 floor Eason0807 reply:
Quote: refer to 4th floor qq_41654492 response:

Quote: refer to the second floor Eason0807 response:


The problem of import, and in the process you use ADO.NET nothing difference between

Does you read the data in the DataTable, then Insert into the database, ADO.NET usage is the same

Is don't want to say anything, you need to read the data from the file in
Don't know what you from file to import, Excel? TXT?


You have said is directly while testing, and then use the insert directly into a a data import is? So how do I select text columns?


Once a cycle to the conventional way, don't suggest you do that, every time to open a connection very consume resources;
Common practice when spelling a SQL statement, but if the data quantity is big, not a whole put together, to segment;
If you use some O/RM framework, the process can be very simple


Thank you , ORM framework does not understand, I went to see, is not I look to the appropriate documentation instructions again, after all beginners, to complete the purpose in the pursuit of efficiency

CodePudding user response:

Microsoft EF, other Dapper, like this, there is a sugar?

After I used two don't know how to specific how, but EF do you about it, it is very simple, the data to get a List , which is attached to the DbContext, then Save it is done

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C #
  • Related