我對C#非常新,所以請原諒我的無知。我有一個SQL Server表,我試圖從該表中將選擇查詢結果轉換爲C#中的表單組合框。C#從SQL Server數據庫獲取結果,通過類顯示在組合框中
我在試圖做的是創建一個類,它將在數據庫端執行一個存儲過程,而我的絆腳石是如何將該類整合到代碼中,以便結果顯示在C#中的下拉列表中。
這是我到目前爲止在C#中的。非常感謝您的幫助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MedicalOffice
{
public class DBAIdSelect
{
public void SelectPractice()
{
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = GetConnectionString();
cn.Open();
using (SqlCommand cmd = new SqlCommand("SelectPracticeID"))
{
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
}
}
private string GetConnectionString()
{
string conString = ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString;
return conString;
}
}
}
SQL Server表:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Practices]
(
[PracticeID] [int] IDENTITY(1,1) NOT NULL,
[PracticeName] [varchar](50) NULL,
[Address1] [varchar](50) NULL,
[Address2] [varchar](50) NULL,
[City] [varchar](50) NULL,
[State] [char](2) NULL,
[Zip] [varchar](10) NULL,
[IsActive] [bit] NULL,
[DateCreated] [date] NULL
CONSTRAINT [DF_Practices_DateCreated] DEFAULT (getdate()),
[CreatedBy] [int] NULL,
[DateModified] [date] NULL,
[DateModifiedBy] [int] NULL,
CONSTRAINT [PK_Practices]
PRIMARY KEY CLUSTERED ([PracticeID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
和存儲過程:
create proc [dbo].[SelectPracticeID]
as
select PracticeID
from dbo.Practices
GO
您沒有提問?但你的下一步將是從你的班級返回值。 'cmd.ExecuteNonQuery();'不會工作 - 您需要返回值。 – Jonesopolis
Webforms,winforms,MVC,WCF?你使用什麼技術? – Liam
您調用存儲來檢索記錄,但您沒有對返回進行任何操作。例如,如果你得到一個datareader,你應該從那裏讀取記錄。連接也沒有關閉。網上有很多關於如何實現你想要實現的代碼示例。 – derloopkat