我正在尝试登录SQL并调用DB storred过程,但是我得到了一些我不太懂的输出。 这是我的代码:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
public partial class Sendcode3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpRequest currentRequest = HttpContext.Current.Request;
String id = Server.UrlEncode(Request.QueryString["id"]);
String pw = Server.UrlEncode(Request.QueryString["pw"]);
String ip = "127.0.0.1";//currentRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];
int ret = 0;
//if (ip == null || ip.ToLower() == "unknown")
// ip = currentRequest.ServerVariables["REMOTE_ADDR"];
//if (id == "")
// Response.Write("-99");
try
{
string connString = ConfigurationManager.ConnectionStrings["RohanUser"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connString);
string query = "call [dbo].[ROHAN3_SendCode](@id,@pw,@ip,@ret)";
SqlCommand myCommand = new SqlCommand(query);
myCommand.Parameters.AddWithValue("id", id);
myCommand.Parameters.AddWithValue("pw", pw);
myCommand.Parameters.AddWithValue("ip", ip);
myCommand.Parameters.Add("ret", SqlDbType.Int).Direction = ParameterDirection.Output;
myCommand.Connection = sqlConn;
sqlConn.Open();
if (myCommand.ExecuteNonQuery() > 0)
{
//ret = int.Parse(myCommand.Parameters["ret"].Value.ToString());
ret = -202;
}
myCommand.Connection.Close();
sqlConn.Close();
Response.Write(ret);
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
我得到的输出如下:
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '.'. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Sendcode3.Page_Load(Object sender, EventArgs e) in c:\Users\Mihai\Documents\Visual Studio 2017\WebSites\RohanAuth\sendcode3.aspx.cs:line 34 ClientConnectionId:860145ab-56e5-496a-8d1a-ea39cf2c93e1 Error Number:102,State:1,Class:15
我错过了什么?我似乎无法弄清楚问题是什么
答案 0 :(得分:0)
问题是添加了架构名称dbo
和call
关键字。您也不需要像这样提及输入参数。剥离出来导致默认模式,你的命令应该是
string query = "[ROHAN3_SendCode]";
有关详细信息,请参阅Google中的How to: Execute a Stored Procedure that Returns Rows(或)搜索问题标题
答案 1 :(得分:0)
这样做:
var cmd = new SqlCommand("your_stored_procedure_name", connString);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
// ...