Bonjour à toutes et à tous,

Je suis actuellement sur un projet où je dois transférer des données venant d'un fichier texte vers une BDD MS SQL.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This sample program shows how to create a database record. */
 
/* define PURE_SQL to use a SQL INSERT INTO statement instead of   */
/* DBCreateRecord and DBPutRecord.                                 */
#undef PURE_SQL
#include "cvi_db.h"
#include <utility.h>
#include <ansi_c.h>
#include <userint.h>
 
void ShowError()
{
    MessagePopup("Database Error",DBErrorMessage());
}
 
 
void main()
{
    int hdbc = 0;       /* Handle to database connection    */
    int hmap = 0;       /* Handle to map                    */
    int hstmt = 0;      /* Handle to SQL statement          */
    char uutNum[11];    /* Buffer for uut serial number     */
    int uutStat;        /* Status variable for uut number   */
    double meas1;       /* Variable for test measurement 1  */
    int meas1Stat;      /* Fetch status  for measurement 1  */
    double meas2;       /* Variable for test measurement 2  */
    int meas2Stat;      /* Fetch tatus for measurement 2    */
    int resCode;        /* Result code                      */
	char dsn[74]	;
 
#ifdef WIN64
	DisableBreakOnLibraryErrors();
#endif 
	/* Connect to database (in this case dBase files) */
    hdbc = DBConnect ("DSN = ");
#ifdef WIN64
	EnableBreakOnLibraryErrors();
	if (hdbc == -10 || hdbc == -11) {
		MessagePopup ("Could not connect to Datasource", 
			"The SQL Toolkit could not connect to the \"CVI SQL Samples\" data source.\n"		\
			"The most likely reason for this is because the SQL Toolkit was unable to\n"		\
			"register the datasource at installation time. The samples.mdb file requires\n" 	\
			"a 64-bit Access driver and as of April 2010 one did not exist. According to\n"		\
			"Microsoft, the Jet Database Engine is being deprecated and will be replaced\n"		\
			"with a new x64 compatible Access Database Engine when Office 2010 is released.\n\n"\
			"Once a 64-bit Access driver has been released and installed on this machine,\n"	\
			"the DSN can be added by opening up Control Panel and then going to\n"				\
			"Administrative Tools >> Data Sources (ODBC). In the System DSN tab, hit the\n"		\
			"Add button and select the Microsoft Access Driver you just installed. You must\n"	\
			"set the Data Source Name as \"CVI SQL Samples\".\n"		\
			"Then Select the samples.mdb database located at:\n"								\
			"C:\\Users\\Public\\Documents\\National Instruments\\CVI\\samples\\sql\\samples.mdb.\n"	\
			"Click the OK button and then the SQL samples should work.\n\n"			\
			"Note: The SQL samples should all work correctly when run in 32-bit mode."
			);
		goto Error;
		}
	else
#endif
    if (hdbc <= 0) {ShowError(); goto Error;}
 
#ifdef PURE_SQL
    resCode = DBImmediateSQL(hdbc, "INSERT INTO TESTRES (UUT_NUM, MEAS1, MEAS2) \
                VALUES ('2860B567',0.7,1.1)");
#else
 
    /* begin map for constructed SQL statement */
    hmap = DBBeginMap (hdbc);
    if (hmap <= 0) {ShowError(); goto Error;}
 
    /* specify the columns to be selected and the variables where column */
    /* values will be placed.                                            */
    resCode = DBMapColumnToChar (hmap, "UUT_NUM", 11, uutNum, &uutStat, "");
    if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
    resCode = DBMapColumnToDouble (hmap, "MEAS1", &meas1, &meas1Stat);
    if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
    resCode = DBMapColumnToDouble (hmap, "MEAS2", &meas2, &meas2Stat);
    if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
 
    /* Activate the map for table testres.  (construct a SQL Select     */
    /* statement, execute the statement, bind the selected columns to   */
    /* the previously specified variables.)                             */
    hstmt = DBActivateMap (hmap, "testres");
    if (hstmt <= 0) {ShowError(); goto Error;}
 
    /* Create the new record */
    resCode = DBCreateRecord (hstmt);
    if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
 
    /* Put values into the bound variables */
    strcpy(uutNum, "2860B567");
    meas1 = 0.7;
    meas2 = 1.1;
 
    /* Insert the record into the database */
    resCode = DBPutRecord (hstmt);
    if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
 
    resCode = DBDeactivateMap (hmap);
    if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
#endif
    resCode = DBDisconnect (hdbc);
    if (resCode != DB_SUCCESS) {ShowError(); goto Error;}
    MessagePopup ("Create Record Sample",
                  "Successfully created record for UUT_NUM 2860B567");
Error:
    return;
}
Exemple donné dans la librairie SQL Tool.

Je ne sais pas comment faire pour me connecter à la BDD en question (le terme DSN apparaît). Et je ne sais ni comment aller lire ce fichier.txt pour transférer les données.

En vous remerciant,
AUFFRET Fabien.