簡體   English   中英

如何在C#中調用SQL函數?

[英]How can I call a SQL function in C#?

我在SQL中創建了一個函數,現在我需要在我的C#應用​​程序中使用該函數。

我嘗試使用這樣的東西,但似乎我做錯了,因為我得到了:

Must declare the scalar value '@2064734117'

...當我將2064734117作為第一個參數並將1作為第二個參數時。 這是我正在談論的代碼:

SqlConnection con = new SqlConnection(clsDb.connectionString);
string query = string.Format("select Function1(@{0},@{1}) ",
  int.Parse(e.CurrentRow.Cells["CodeMeli"].Value.ToString()),1);
con.Open();
SqlCommand cmd = new SqlCommand(query,con);
SqlDataAdapter READER = new SqlDataAdapter();
READER.SelectCommand = cmd;
DataTable table = new DataTable();
READER.Fill(table);
radGridView1.DataSource = table;
con.Close();

我的函數接受兩個整數參數並返回一個表。 我在Visual Studio中檢查了它並且它有效,但我無法在我的應用程序中使用它。

這是我的功能聲明:

ALTER FUNCTION dbo.Function1
(
/*
@parameter1 int = 5,
@parameter2 datatype
*/
@ID int,
@clsTypeID int
)
    RETURNS TABLE/* @table_variable TABLE (column1 datatype, column2 datatype) */
    AS
         /*BEGIN */
    /* INSERT INTO @table_variable
       SELECT ... FROM ... */
RETURN SELECT  * FROM tblCLASS2 
        WHERE STNID = @ID AND CLASSTYPEID =  @clsTypeID  
/*END */
/*GO*/

你的SQL有點偏,它應該是:

  string query = string.Format("select * from dbo.Function1({0},{1});", int.Parse(e.CurrentRow.Cells["CodeMeli"].Value.ToString()),1);

您可能希望使用SqlParameter對象來阻止sql注入:

  string query = "select * from dbo.Function1(@pa1,@par2);";
  cmd.Parameters.Add("@par1", SqlDbType.Int).Value = int.Parse(e.CurrentRow.Cells["CodeMeli"].Value.ToString());  
  cmd.Parameters.Add("@par2", SqlDbType.Int).Value = 1;

一目了然,我能看到的第一件事是你沒有指定對象所有者/架構; 這是函數所必需的,所以應該select dbo.Function1(...

第二:看看你對string.Format的調用是什么; 正在生成@1@nn另一個整數,但這並不是一個有效的參數名稱。 這很方便,因為

第三:你沒有添加任何參數

第四:對於表UDF(而不是標量UDF),必須select * from dbo.Function1(... ,而不僅僅是select dbo.Function1(...

你可以這樣做:

        myConn.Open();

        //generating the new command for our database
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT OBJECTID_1, NDNT as theAddress, MIN(ABS(x - " + double.Parse(x.ToString()) + ") + ABS(y - " + double.Parse(y.ToString()) +")) from dbo.DWH_OUTPUT  GROUP BY OBJECTID_1,NDNT HAVING   (MIN(ABS(x - " + double.Parse(x.ToString()) + ") + ABS(y - " + double.Parse(y.ToString()) + ")) = (Select MIN(ABS(a.x - " + double.Parse(x.ToString()) + ") + ABS(a.y - " + double.Parse(y.ToString()) + ")) from dbo.DWH_OUTPUT a ) )";
        cmd.Connection = myConn;

        //getting some more ado.net objects
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();

        da.SelectCommand = cmd;
        da.Fill(ds, @"Addresses");

        if (ds.Tables[0].Rows.Count > 0)
        {
            theAddress = ds.Tables[0].Rows[0][@"theAddress"] + @" (proximity address)";
        }

        myConn.Close();

請注意,在此示例中,您將SqlCommand的CommandType設置為CommandType.Text 指定命令參數(即代碼段中的select函數),然后使用Fill方法填充數據集。 然后,您可以像通常使用標准ado.net一樣從行中獲取值。

如果你需要調用存儲過程,請看一下:

如何從ado.net調用TSQL函數

您需要具有所有者/架構名稱的完全限定功能名稱以下鏈接提供的工作示例:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM