SQL 中的存盤程序與所需的結果完美配合。
ALTER PROCEDURE [dbo].[My_StoredProc]
(@autoidx int,
@r varchar(max) OUTPUT)
--with encryption
AS
BEGIN
-- ... some code here ...
END
嘗試使用 ODBC 命令在 VS 2019 中呼叫存盤程序,但出現錯誤:
程序或函式 My_StoredProc 需要引數 @autoidx,但未提供該引數。
我的代碼:
string connectionString = String.Format("DSN={0};uid=my_user;pwd=my_pwd", toolStripComboBoxDSN.Text);
OdbcCommand DbCommand = new OdbcCommand("My_StoredProc", DbConnection);
DbCommand.CommandType = CommandType.StoredProcedure;
DbCommand.Parameters.AddWithValue("@autoidx", this.AutoIndex); //this.AutoIndex has value
DbCommand.Parameters.Add("@r", OdbcType.VarChar,500);
DbCommand.Parameters["@r"].Direction = ParameterDirection.Output;
DbConnection.Open();
DbCommand.ExecuteNonQuery();
string s = (string)DbCommand.Parameters["@r"].Value;
uj5u.com熱心網友回復:
我有一個意見:
1.-嘗試改變這一點
DbCommand.Parameters.AddWithValue("@autoidx", this.AutoIndex);
為了這:
DbCommand.Parameters.Add("@autoidx", SqlDbType.Int);
DbCommand.Parameters["@autoidx"].Value = this.AutoIndex;
2.- 檢查您的 DbConnection,有時這包含其他資料庫,例如 db_developer 而不是 db_production。
uj5u.com熱心網友回復:
嘗試使用SQL Server 的System.Data.SqlClient 本機SQL 客戶端,并使用適當的using塊和適當的引數定義 - 如下所示:
// standard SQL Client connection string
string connectionString = "......";
// define connection and command in proper "usinh" blocks
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand("dbo.My_StoredProc", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
// define parameters properly
cmd.Parameters.Add("@autoidx", SqlDbType.Int);
cmd.Parameters.Add("@r", SqlDbType.VarChar, -1).Direction = ParameterDirection.Output;
// set values to parametesr
cmd.Parameters["@autoidx"] = this.AutoIndex;
// open connection, execute procedure, close connection
conn.Open();
int rowsAffected = cmd.ExecuteNonQuery();
string s = cmd.Parameters["@r"].Value?.ToString();
conn.Close();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410561.html
標籤:
