我对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