.Net下執(zhí)行sqlcmd的方法
更新時間:2010年06月11日 01:17:39 作者:
遇到這樣一個問題:程序?qū)崿F(xiàn)創(chuàng)建sqlserver 端點,添加webmethod,刪除webmethod,如果直接用ado.net 的sqlhelp 實現(xiàn),總是報錯,后來想到了用sqlcmd
如下代碼:
被的調(diào)用方法:
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
調(diào)用方法:
protected void Button1_Click(object sender, EventArgs e)
{
string sqlQuery = "sqlcmd.exe -U sa -P 123 -S 20100330-0922 -d test -i c:\\1.sql";
string strRst = ExeCommand(sqlQuery);
}
1.sql文件
use master
go
CREATE ENDPOINT Orders_Endpoint6
state=started
as http(
path='/sql/orders6',
AUTHENTICATION=(INTEGRATED),
ports=(clear)
)
for soap(
WebMethod 'CustOrdersOrders'(
name='test.dbo.GetAlltb12'
),
wsdl=default,
database='test',
namespace='http://mysite.org/'
)
BS程序如果執(zhí)行的話,客戶端不安裝sqlcmd不知能否運行?
被的調(diào)用方法:
復(fù)制代碼 代碼如下:
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
調(diào)用方法:
復(fù)制代碼 代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
string sqlQuery = "sqlcmd.exe -U sa -P 123 -S 20100330-0922 -d test -i c:\\1.sql";
string strRst = ExeCommand(sqlQuery);
}
1.sql文件
復(fù)制代碼 代碼如下:
use master
go
CREATE ENDPOINT Orders_Endpoint6
state=started
as http(
path='/sql/orders6',
AUTHENTICATION=(INTEGRATED),
ports=(clear)
)
for soap(
WebMethod 'CustOrdersOrders'(
name='test.dbo.GetAlltb12'
),
wsdl=default,
database='test',
namespace='http://mysite.org/'
)
BS程序如果執(zhí)行的話,客戶端不安裝sqlcmd不知能否運行?
相關(guān)文章
SQL Server 不存在或訪問被拒絕(轉(zhuǎn))
在使用 SQL Server 的過程中,用戶遇到最多的問題莫過于連接失敗了。一般而言,有兩種連接SQL Server 的方式,一是利用 SQL Server 自帶的客戶端工具2009-06-06
CentOS安裝SQL Server vNext CTP1教程
這篇文章主要為大家詳細介紹了CentOS上安裝SQL Server vNext CTP1的相關(guān)過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
SQL Server 實現(xiàn)數(shù)字輔助表實例代碼
這篇文章主要介紹了SQL Server 實現(xiàn)數(shù)字輔助表的相關(guān)資料,并附實例代碼,需要的朋友可以參考下2016-10-10
LINQ to SQL:處理char(1)字段的方式會引起全表掃描問題
1.相關(guān)內(nèi)容: 在SQL Server 2000中,如果數(shù)據(jù)庫的排序規(guī)則為Chinese_PRC_CI_AS,那么查詢時是不分大小寫的,例如下列這二條SQL語句,查詢的結(jié)果是一樣的。2008-03-03
sql注入過程詳解_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細介紹了sql注入過程的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

