.Net下執(zhí)行sqlcmd的方法
更新時間:2010年06月11日 01:17:39 作者:
遇到這樣一個問題:程序?qū)崿F(xiàn)創(chuàng)建sqlserver 端點(diǎn),添加webmethod,刪除webmethod,如果直接用ado.net 的sqlhelp 實(shí)現(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不知能否運(yùn)行?
被的調(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不知能否運(yùn)行?
相關(guān)文章
SQL Server 不存在或訪問被拒絕(轉(zhuǎn))
在使用 SQL Server 的過程中,用戶遇到最多的問題莫過于連接失敗了。一般而言,有兩種連接SQL Server 的方式,一是利用 SQL Server 自帶的客戶端工具2009-06-06CentOS安裝SQL Server vNext CTP1教程
這篇文章主要為大家詳細(xì)介紹了CentOS上安裝SQL Server vNext CTP1的相關(guān)過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11SQL Server 實(shí)現(xiàn)數(shù)字輔助表實(shí)例代碼
這篇文章主要介紹了SQL Server 實(shí)現(xiàn)數(shù)字輔助表的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-10-10LINQ to SQL:處理char(1)字段的方式會引起全表掃描問題
1.相關(guān)內(nèi)容: 在SQL Server 2000中,如果數(shù)據(jù)庫的排序規(guī)則為Chinese_PRC_CI_AS,那么查詢時是不分大小寫的,例如下列這二條SQL語句,查詢的結(jié)果是一樣的。2008-03-03sql注入過程詳解_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了sql注入過程的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08列出SQL Server中具有默認(rèn)值的所有字段的語句
上個星期我在對一個供應(yīng)商開發(fā)的數(shù)據(jù)庫按規(guī)定進(jìn)行故障排除的時候,我們需要對他們數(shù)據(jù)庫中50個表的每一個都進(jìn)行查看,以確保所有期望是默認(rèn)值的字段都被分配了默認(rèn)值。你可以想象這是一個多么令人畏懼的工作,而我立即提出了這個問題。有沒有一個比在SQL Server管理套件中打開每一個表來查看這個schema的更好方法嗎?2008-10-10