c# - Why is my bcp query not working? -
i'm beginner in c#, want use bcp
utility in c# , so, wrote code:
string connstr = "data source=192.168.50.172;initial catalog=cdrdb;user id=cdrlogin;password=beh1368421"; //string connstr = "enter connection string here"; system.diagnostics.process proc = new system.diagnostics.process(); proc.startinfo.filename = "bcp"; proc.startinfo.arguments = @"select * cdrtable"" queryout c:\filename.csv -s 192.168.50.172 -u cdrlogin -p beh1368421 -f -w"; proc.start();
that code runs, filename.csv
not created on c:\
drive. happened? how can solve problem?
for bcp
executing system.diagnostics.process()
, 1 should use -c
, -craw
options.
below sample code.
string execpath = @"..\bcp.exe"; string localfile = @"metadata.table"; string localpath = @"..\outputfolder"; system.diagnostics.process p = new system.diagnostics.process(); p.startinfo.useshellexecute = false; p.startinfo.redirectstandardoutput = true; p.startinfo.redirectstandarderror = true; p.startinfo.createnowindow = true; p.startinfo.filename = execpath; p.startinfo.arguments = string.concat("\"", "metadata.table", "\" out \"", localpath, "\\", localfile, ".dat\" -c -c raw -t\"", separator, "\" -s \"", "(local)", "\" -t"); p.start(); p.waitforexit(); p.close(); p.dispose();
you can use bcp in c# code using bulk insert
transact-sql statement , using executenonquery
method of sqlcommand
.
something below work:
scommandtext = "exec xp_cmdshell 'bcp.exe'"+database + ".." + tablename + " in " + filename + " -c -q -u " + userid + " -p " + password + "-t "; sqlconnection conn = new sqlconnection(connectionstring); conn.open(); sqlcommand cmd = new sqlcommand(); cmd.connection = conn; cmd.commandtype = commandtype.text; cmd.commandtext = scommandtext; return cmd.executenonquery();
Comments
Post a Comment