[英]Change dataset binding from table to stored procedure
這是一個Windows應用程序。 最初,我有一個表的下拉菜單的數據集。 現在,我想使用存儲過程。 如何在代碼中修改過程?
我認為最好的方法可能是刪除數據集並重新創建一個新的數據集。 但是我們可以在設計器代碼中做嗎?
謝謝。
編輯
protected Problem_DE_DataSet(global::System.Runtime.Serialization.SerializationInfo info, global::System.Runtime.Serialization.StreamingContext context) :
base(info, context, false) {
if ((this.IsBinarySerialized(info, context) == true)) {
this.InitVars(false);
global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler1 = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
this.Tables.CollectionChanged += schemaChangedHandler1;
this.Relations.CollectionChanged += schemaChangedHandler1;
return;
}
string strSchema = ((string)(info.GetValue("XmlSchema", typeof(string))));
if ((this.DetermineSchemaSerializationMode(info, context) == global::System.Data.SchemaSerializationMode.IncludeSchema)) {
global::System.Data.DataSet ds = new global::System.Data.DataSet();
ds.ReadXmlSchema(new global::System.Xml.XmlTextReader(new global::System.IO.StringReader(strSchema)));
if ((ds.Tables["Problem_DE"] != null)) {
base.Tables.Add(new Problem_DEDataTable(ds.Tables["Problem_DE"]));
}
this.DataSetName = ds.DataSetName;
this.Prefix = ds.Prefix;
this.Namespace = ds.Namespace;
this.Locale = ds.Locale;
this.CaseSensitive = ds.CaseSensitive;
this.EnforceConstraints = ds.EnforceConstraints;
this.Merge(ds, false, global::System.Data.MissingSchemaAction.Add);
this.InitVars();
}
else {
this.ReadXmlSchema(new global::System.Xml.XmlTextReader(new global::System.IO.StringReader(strSchema)));
}
this.GetSerializationData(info, context);
global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
base.Tables.CollectionChanged += schemaChangedHandler;
this.Relations.CollectionChanged += schemaChangedHandler;
}
使用存儲過程只會給您另一個表。 如果使用數據集設計器,只需創建一個新的tableadapter並將存儲過程用作select語句。
如果這不是您要的內容,請使用一些代碼更新您的問題。
代碼很容易。 暗示您的SQLCommand稱為myCommand
將myCommand.Text
更改為存儲過程的名稱。
將myCommand.CommandType
更改為CommandType.StoredProcedure
。
對於存儲過程中包含的每個參數,請使用以下行:
myCommand.Parameters.AddWithCalue("@YourSQLParameter",YourValue)
我喜歡將DataReaders
用於此類操作。
SQLDataReader myReader = myCommand.ExecuteReader();
瞧! 您的StoredProcedure已執行。
現在,假設您要將添加過程的結果添加到ComboBox
。
while (myReader.Read()) { myComboBox.Items.Add(myReader["ColumnName"].Tostring(); }
基本示例,但我敢肯定您的意思。 如果您需要更多信息,可以隨時閱讀本教程,它幾乎可以解釋您想要的內容。
更新:
您在此處張貼的生成代碼很混亂,但是方法仍然相同。 據我了解,您只想使用存儲過程從表中讀取內容,並使用特定字段填充ComboBox或DropDownList。 您應該嘗試在代碼部分從頭開始輸入它,而無需使用該設計器來了解它的工作原理。
您應該有這樣的東西:
//Creates a connection to your DataBase
SqlConnection myConnection = new SqlConnection(@"Server=YOURSERVER;Database=YOURDATABASE;User id=YOURID; Password=YOURPASSWORD");
//Opens the connection
myConnection.Open();
//Creates a command (Query)
SqlCommand myCommand = myConnection.CreateCommand();
//Sets the type of query to Stored Procedure (EXEC ...)
myCommand.CommandType = CommandType.StoredProcedure;
//The Query is set to stored procedure so (EXEC THE_NAME_OF_YOU_STOREDPROCEDURE)
myCommand.CommandText = "THE_NAME_OF_YOUR_STOREDPROCEDURE";
//This will add each parameter to your query (EXEC THE_NAME_OF_YOURSTOREDPROCEDURE @YOURPARAMETER
myCommand.Parameters.AddWithValue("@YOURPARAMETER", THE_VALUE_OF_THE_PARAMETER);
SqlDataReader myReader = myCommand.ExecuteReader();
//For each records returned the item from the ["YOURCOLUMN"] will be added to the comboBox
while(myReader.Read())
{
myComboBox.Items.Add(myReader["YOUR_COLUMN_NAME"]);
}
myReader.Close();
myConnection.Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.