2015-01-07 176 views
0

試圖創建一個asp.net c#表格用於學習目的在家裏,我絕對努力連接到我的storedprocedure。存儲過程中的SQL Server和使用存儲過程

我肯定是在正確的數據庫中,因爲當我通過cmdprompt啓動數據庫並通過數據源連接到它時,它在視覺設計中連接並查找存儲過程。所以一定有我做錯的事情?我一直在Google上搜索大約30-40分鐘,我嘗試過的所有內容都沒有解決我的問題。有什麼建議嗎?

const string constring = @"Data Source=(localdb)\ProjectsV12;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"; 

    public static int InsertEnquiry(string Name, string Subject, string Email, string Message, string Phone) { 

     //default ticketid of 0 which will be changed to -1 if an error or greater than 0 if successful 
     int TicketID = 0; 

     if (String.IsNullOrEmpty(Phone)) Phone = "0"; 

     using (var conn = new SqlConnection(constring)) { 
      //create command 
      SqlCommand command = new SqlCommand(); 

      //tell the command which connection its using 
      command.Connection = conn; 

      //inform command that it is a stored procedure and the name of stored procedure 
      command.CommandText = "InsertEnquiry"; 
      command.CommandType = CommandType.StoredProcedure; 

      //add the parameters to the sqlcommand 
      command.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar)).Value = Name; 
      command.Parameters.Add(new SqlParameter("@Subject", SqlDbType.NVarChar)).Value = Subject; 
      command.Parameters.Add(new SqlParameter("@Phone", SqlDbType.NVarChar)).Value = Phone; 
      command.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar)).Value = Email; 
      command.Parameters.Add(new SqlParameter("@Message", SqlDbType.NVarChar)).Value = Message; 

      // try run command and set TicketID to the row inserted into the table. 
      try { 
       conn.Open(); 
       //run command 
       command.ExecuteNonQuery(); 

       //return scope identity of row. 
       TicketID = (int)command.Parameters["@TicketID"].Value; 
      } 
      catch (Exception e) { 
       //show -1 to state there is an error 
       TicketID = -1; 
      } 
     } 

     return TicketID; 
    } 
} 
+0

這裏有一個MSDN文章。您可能需要向下滾動。 [使用帶有SqlCommand和存儲過程的參數](http://msdn.microsoft.com/zh-cn/library/yy6y35y8(v = vs.110).aspx) – Cameron

+0

沒有SQL Server 2013版本...我們有SQL Server 2012和2014 ....你使用哪一個? –

+0

對不起 - 它的視覺快速2013年使用SQL服務器內 - 我會修改標題如果可能的話。 – cooLLedge

回答

0

1st 我認爲你連接到錯誤的分貝,可能你是在主人。 運行這段代碼並檢查數據庫的名稱,看看它是否是你想要的。

public static string checkDB() 
{ 
    string dbName = ""; 
    using (var conn = new SqlConnection(constring)) 
    { 
     //create command 
     SqlCommand command = new SqlCommand(); 

     //tell the command which connection its using 
     command.Connection = conn; 

     //inform command that it is a stored procedure and the name of stored procedure 
     command.CommandText = "select DB_NAME()"; 
     command.CommandType = CommandType.Text; 

     // try run command and set TicketID to the row inserted into the table. 
     try 
     { 
      conn.Open(); 
      //run command 
      SqlDataReader reader = command.ExecuteReader(); 
      reader.Read(); 
      dbName = reader[0].ToString(); 
     } 
     catch (Exception e) 
     { 
     } 
    } 

    return dbName; 
} 

第二 您正在試圖從參數@TicketID價值,但你沒有指定這個參數作爲輸出參數。

command.Parameters.Add("@TicketID", SqlDbType.Int).Direction = ParameterDirection.Output; 

EDIT1: 這是你如何在連接字符串中把數據庫名:

const string constring = @"Data Source=(localdb)\ProjectsV12;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;Initial Catalog=MY_DB_NAME"; 
+0

非常感謝!我只知道我錯過了一些東西!你是對的 - 它沒有連接到正確的目錄。 – cooLLedge