2017-07-08 41 views
-2

在VB.net中,我可以簡單地在SQL Server 2008中使用下面的圖像查詢執行存儲過程,但在C#中出現錯誤。使用C#中的表格適配器執行存儲過程

你能幫我一下C#中的正確語法嗎?

感謝

enter image description here

+0

顯示我們的實際VB代碼,不粘貼到一個C#類的VB代碼。 – mjwills

+0

而不是張貼執行代碼發佈代碼的屏幕截圖。 –

+0

這是C#中的代碼,我使用的數據綁定已經解決了 –

回答

1
using (SqlConnection conn = new SqlConnection(your-connection-string)) { 
conn.Open(); 

// 1. create a command object identifying the stored procedure 
SqlCommand cmd = new SqlCommand("your-procedure-name", conn); 

// 2. set the command object so it knows to execute a stored procedure 
cmd.CommandType = CommandType.StoredProcedure; 

// 3. add parameter to command, which will be passed to the stored procedure 
cmd.Parameters.Add(new SqlParameter("@Username", textBox1.Text)); 
cmd.Parameters.Add(new SqlParameter("@Password", textBox2.Text)); 

// execute the command 
using (SqlDataReader result = cmd.ExecuteReader()) { 
    // iterate through results, printing each to console 
    while (result .Read()) 
    { 
     // Name and Password Should Match with your proc col name 
     var userName = result["Name"].toString(); 
     var password = result["password"].toString(); 

    } 
} 
} 

更多詳情,請閱讀本該

How to execute a stored procedure within C# program