将变量插入SQL数据库

时间:2014-11-13 13:04:16

标签: c# sql-server web-services

是webservices和SQL的新手。我开发了一个插入SQL数据库的Web服务。下面是我的代码。

 public class balanceEnquiry : System.Web.Services.WebService
 {
    [WebMethod(MessageName = "CheckBalance")]//To Check Balance......
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(Action = "CheckBalance", RequestNamespace = "http://www.zain.com/", ResponseNamespace = "http://www.zain.com/", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string CheckBalance(string AccountNumber, string msisdn, string pin)
    {
        ValidationAPIClient client = new ValidationAPIClient();

        //validate sub by pin
        //fetch MAI by msisdn
        //validate pin

我想将用户输入的msisdn插入我的数据库。以及合作伙伴API对我的网络服务的响应如下

 return validateAccountResultItem.responseExtraData;

我想在我的数据库中插入2。我将如何编写插入语句。以下是我一直在做的事情

cmd.CommandText = string.Format("INSERT INTO MSSG(REFERENCE_ID, TIMESTAMP, DEST_MSISDN, MESSAGE, TYPE, HOSTIP) VALUES ('1234567890', SYSDATE,'91976707759','TESTMESSAGE','0',(SELECT HOSTIP FROM SMPP_TRANSMITTER WHERE STATUS = 1 AND ROWNUM = 1))");

我希望它们分别是用户输入的字符串和validateAccountResultItem.responseExtraData,而不是硬编码DEST_MSISDN和MESSAGE列。

1 个答案:

答案 0 :(得分:1)

cmd.CommandText = string.Format("INSERT INTO MSSG(REFERENCE_ID, TIMESTAMP, DEST_MSISDN, MESSAGE, TYPE, HOSTIP) VALUES ('1234567890', SYSDATE,@DEST_MSISDN,@MESSAGE,'0',(SELECT HOSTIP FROM SMPP_TRANSMITTER WHERE STATUS = 1 AND ROWNUM = 1))");

String destMsisdn, message;

//**todo** here, retrieve user input in those two variables


cmd.Parameters.AddWithValue("@DEST_MSISDN", destMsisdn);
cmd.Parameters.AddWithValue("@MESSAGE", message);