2013-06-20 27 views
0

我使用Visual Studio 2010,C#語言和SQL Server Express Edition作爲後端。 DataGridView控件有一種形式,我使用設計器手動設置列,然後在運行時設置數據源。帶有標識列的C#DataGridView

connectionString = ConfigurationManager.AppSettings["connectionString"]; 
sqlConnection = new SqlConnection(connectionString); 
sqlConnection.Open(); 
String sqlSelectDet = "Select OrdDetID, OrderID, ProductID, UnitPrice, Quantity from OrderDetails"; 

//=============================================================================== 
//--- Set up the INSERT Command OrderDetails 
//=============================================================================== 

sDetInsProcName = "prInsert_OrderDetail"; 
insertcommandDet = new SqlCommand(sDetInsProcName, sqlConnection); 
insertcommandDet.CommandType = CommandType.StoredProcedure; 

insertcommandDet.Parameters.Add("@nNewDetID", SqlDbType.Int, 4, "OrdDetID"); 
insertcommandDet.Parameters.Add("@nOrderID", SqlDbType.Int, 4, "OrderID"); 
insertcommandDet.Parameters.Add("@nProductID", SqlDbType.Int, 4, "ProductID"); 
insertcommandDet.Parameters.Add("@mUnitPrice", SqlDbType.Money, 8, "UnitPrice"); 
insertcommandDet.Parameters.Add("@nQuantity", SqlDbType.SmallInt, 2, "Quantity"); 

sqlDataDet.InsertCommand = insertcommandDet; 

正如代碼中提到的,我使用的是存儲過程prInsert_OrderDetailOrdDetID是一種LineItem編號,它是數據庫中的自動增量字段。

有關設置的DataGridView我使用下列內容:

dtDet = new DataTable(); 
dtDet.Clear(); 
sqlDataDet.FillSchema(dtDet, SchemaType.Source); 

ds = new DataSet(); 
ds.Tables.Add(dtDet); 

ds.Tables[1].Columns["OrdDetID"].AutoIncrement = true; 
ds.Tables[1].Columns["OrdDetID"].AutoIncrementSeed = -1; 
ds.Tables[1].Columns["OrdDetID"].AutoIncrementStep = -1; 

bsDet = new BindingSource(); 
bsDet.DataSource = ds; 
bsDet.DataMember = "OrderDetails"; 

// Name of DataGridView Control is dgInvDet 
dgInvDet.AutoGenerateColumns = false; 
dgInvDet.DataSource = bsDet; 
dgInvDet.Columns["OrdDetID"].DataPropertyName = "OrdDetID"; 
dgInvDet.Columns["OrderID"].DataPropertyName = "OrderID"; 
dgInvDet.Columns["ProductID"].DataPropertyName = "ProductID"; 
dgInvDet.Columns["UnitPrice"].DataPropertyName = "UnitPrice"; 
dgInvDet.Columns["Quantity"].DataPropertyName = "Quantity"; 

在保存按鈕的單擊事件,下面的代碼是使用將數據保存從DataGridView到數據庫:

sqlDataDet.Update(ds.Tables[0]); 

存儲過程在SQL Server中是這樣的:

ALTER PROCEDURE [dbo].[prInsert_OrderDetail] 
@nOrderID INT, 
@nProductID INT, 
@mUnitPrice MONEY, 
@nQuantity SMALLINT, 
@nNewDetID INT OUTPUT 
AS 
INSERT INTO [OrderDetails] (OrderID, ProductID, UnitPrice, Quantity) 
VALUES (@nOrderID, @nProductID, @mUnitPrice, @nQuantity) 

    SET @nNewDetID = SCOPE_IDENTITY() 

數據被正確保存在數據庫中,我問題是我如何獲得OrdDetID實際標識值,dataGridView在數據庫中插入記錄後並未在DataGridView中顯示實際值。由於列的設置,我得到的是-1,-2,-3等等。

任何意見/建議?

感謝

艾哈邁德

回答

1

此參數

insertcommandDet.Parameters.Add("@nNewDetID", SqlDbType.Int, 4, "OrdDetID"); 

需要被指定爲ParameterDirection.Output,即像下面的代碼行:

 
    SqlParameter pID = new SqlParameter("nNewDetID", DBType.Int32, 4); 
    pID.Direction = ParameterDirection.Output; 
    insertcommandDet.Parameters.Add(pID); 
    //** other code here... 
    insertcommandDet.ExecuteScalar(); 

在你執行後你e命令,提取由

int id = Convert.ToInt32(cmd.Parameters["nNewDetID"].Value.ToString()); 

值你應該能夠在一個控制檯應用程序來測試這一點,讓你去...

 
      // set connection 
      SqlConnection sqlConnection = new SqlConnection("[Your connection string here]"); 

      // open connection 
      sqlConnection.Open(); 

      // specify the stored procedure which has an output parameter, namely "NewId" 
      SqlCommand insertcommandDet = new SqlCommand("[dbo].[TestingSP]", sqlConnection); 

      // tell the command that it is a stored procedure 
      insertcommandDet.CommandType = CommandType.StoredProcedure; 

      // add the parameter having the name 
      insertcommandDet.Parameters.AddWithValue("Name","Test Name"); 

      // this parameter will contain the new id after 
      SqlParameter pID = new SqlParameter("NewId", SqlDbType.Int, 4); 
      pID.Direction = ParameterDirection.Output; 

      // add the parameter to the command 
      insertcommandDet.Parameters.Add(pID); 

      // execute command with ExecuteScalar() 
      insertcommandDet.ExecuteScalar(); 

      // Get parameter value extracted with ExecuteScalar 
      int id = Convert.ToInt32(insertcommandDet.Parameters["NewId"].Value.ToString()); 

我使用的過程如下

 
create PROCEDURE [dbo].[TestingSP](
@Name varchar(50) , 
@NewId INT OUTPUT) 
AS 
INSERT INTO [Testing] 
select @name 
    SET @NewId = SCOPE_IDENTITY() 
+0

感謝您的回覆,我應該在哪裏放insertcommandDet.ExecuteScaler()?在保存按鈕點擊事件?或者我已經設置了InsertCommand? – Ahmed

+0

也應該使用sqlDataDet.Update語句,我在改變你建議的代碼後,目前在點擊按鈕事件中使用? – Ahmed

+0

我更新了我的答案。 – Mez