更新时间:2021-10-07 05:43:39
1)创建此程序或相应修改您的SP
1) Create this procedure or modify your SP accordingly
-- ==================================
-- Alter Stored Procedure Template
-- ==================================
ALTER PROCEDURE GetURL
(@Username Nvarchar(50),@url NVARCHAR(MAX)= NULL OUTPUT)
AS
SELECT @url=
'http://api.thirdparty.com/http/sendmsg?user=' + username + '&password=' + password + '&api_id=' + api_id + '&to=081234567&text=Sending+http+from+sql'
FROM Userinfo WHERE username=@Username
select @url url
GO
2)将代码更新为广告d这个
2) Update your code to add this
SqlDataReader adm = null;
string outurl;
//http://www.codeproject.com/Answers/315162/how-to-create-gridview-which-has-column-and-row/?cmt=207083#cmt2_315162
SqlConnection myConnection = new SqlConnection("Data Source=MDT765;Initial Catalog=TST;User Id=sa;Password=sa@123");
myConnection.Open();
SqlCommand cmd = new SqlCommand("GetURL", myConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("@username", SqlDbType.NVarChar,50));
cmd.Parameters["@username"].Value = "user1";
SqlParameter url = new SqlParameter("@url", SqlDbType.NVarChar);
// Type=null;
url.Direction = ParameterDirection.InputOutput;
url.Value = null;
url.Size = 255;
cmd.Parameters.Add(url);
adm = cmd.ExecuteReader();
if (adm.HasRows)
{
while (adm.Read())
{
outurl = adm["url"].ToString();
Response.Redirect(outurl);
}
}
感谢您的回复;它帮助了很多。我不得不修改代码以使用Windows应用程序。这里是c#中的最终代码。如上所述,发送的字符串是SQL中的buildt。我使用Clickatell.com作为服务提供商。
Thanks for the reply; it helped a lot. I had to modify the code a bit to work from Windows app. Herewith the final code in c#. The string that is sent is buildt in SQL as described above. I use Clickatell.com as service providor.
//Needs proxy authentication
WebProxy proxy = new WebProxy(@"10.35.0.22:8080");
proxy.Credentials = new NetworkCredential("username", "password", "domain");
WebRequest.DefaultWebProxy = proxy;
//*** This does not work with IE - get garbage in response - will figure out //*** later
//WebProxy proxy = new WebProxy();
//proxy.Credentials = CredentialCache.DefaultCredentials;
//WebRequest.DefaultWebProxy = proxy;
SqlDataReader adm = null;
string outurl;
SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=xxxxx;User Id=xxxxx;Password=xxxxx");
myConnection.Open();
SqlCommand cmd = new SqlCommand("GetURL", myConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 50));
cmd.Parameters["@username"].Value = "xxxx";
SqlParameter url = new SqlParameter("@url", SqlDbType.NVarChar);
// Type=null;
url.Direction = ParameterDirection.InputOutput;
url.Value = null;
url.Size = 255;
cmd.Parameters.Add(url);
adm = cmd.ExecuteReader();
if (adm.HasRows)
{
while (adm.Read())
{
outurl = adm["url"].ToString();
//changed for winforms app
WebRequest request = WebRequest.Create(outurl);
WebResponse response = request.GetResponse();
MessageBox.Show(response.ResponseUri.ToString());
}
}
在创建之前,我们需要在所需的sqlserver用户下将permision授予以下系统对象。
sp_OASetProperty
sp_OAMethod
sp_OAGetErrorInfo
sp_OADestroy
sp_OAStop
sp_OACreate
sp_OAGetProperty
Sqlserver存储过程
创建程序[dbo]。[HTTP_DB_ALERT](@ srrl varchar(2000),@ response varchar(8000)
out)
As
声明
@obj int
,@ hr int
,@ status int
,@ msg varchar( 255)
exec @hr = sp_OACreate'MSXML2.ServerXMLHttp',@ obj OUT
if @ hr<> 0开始Raiserror('sp_OACreate MSXML2.ServerXMLHttp.3.0
失败',16,1)返回结束
exec @hr = sp_OAMethod @ obj,'open',NULL,'POST',@ srrl,false
如果@hr<> 0开始设置@msg ='sp_OAMethod打开失败'goto eh end
exec @hr = sp_OAMethod @obj,'setRequestHeader',NULL,'Content-Type',
'application / x-www-form-urlencoded'
如果@hr<> 0开始设置@msg ='sp_OAMethod setRequestHeader failed'goto
eh end
exec @hr = sp_OAMethod @obj,send,NULL,''
如果@hr<> 0开始设置@msg ='sp_OAMethod发送失败'转到eh end
exec @hr = sp_OAGetProperty @obj,'status',@ status OUT
如果@hr<> 0开始设置@msg ='sp_OAMethod阅读状态失败'转到
eh
结束
如果@status<> 200开始设置@msg ='sp_OAMethod http状态'+
str(@status)goto eh end
exec @hr = sp_OAGetProperty @ obj,'responseText',@ response OUT
如果@hr<> 0开始设置@msg ='sp_OAMethod读取响应失败'goto
eh end
exec @hr = sp_OADestroy @obj
返回
eh:
exec @hr = sp_OADestroy @obj
Raiserror(@ msg,16,1)
return
Before the creation we nedd to Grant permision to following system object under desired sqlserver user.
sp_OASetProperty
sp_OAMethod
sp_OAGetErrorInfo
sp_OADestroy
sp_OAStop
sp_OACreate
sp_OAGetProperty
Sqlserver Stored Procedure
Create procedure [dbo].[HTTP_DB_ALERT]( @sUrl varchar(2000), @response varchar(8000)
out)
As
Declare
@obj int
,@hr int
,@status int
,@msg varchar(255)
exec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
if @hr <> 0 begin Raiserror('sp_OACreate MSXML2.ServerXMLHttp.3.0
failed', 16,1) return end
exec @hr = sp_OAMethod @obj, 'open', NULL, 'POST', @sUrl, false
if @hr <>0 begin set @msg = 'sp_OAMethod Open failed' goto eh end
exec @hr = sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type',
'application/x-www-form-urlencoded'
if @hr <>0 begin set @msg = 'sp_OAMethod setRequestHeader failed' goto
eh end
exec @hr = sp_OAMethod @obj, send, NULL, ''
if @hr <>0 begin set @msg = 'sp_OAMethod Send failed' goto eh end
exec @hr = sp_OAGetProperty @obj, 'status', @status OUT
if @hr <>0 begin set @msg = 'sp_OAMethod read status failed' goto
eh
end
if @status <> 200 begin set @msg = 'sp_OAMethod http status ' +
str(@status) goto eh end
exec @hr = sp_OAGetProperty @obj, 'responseText', @response OUT
if @hr <>0 begin set @msg = 'sp_OAMethod read response failed' goto
eh end
exec @hr = sp_OADestroy @obj
return
eh:
exec @hr = sp_OADestroy @obj
Raiserror(@msg, 16, 1)
return