使用String Exec从存储过程填充comboBox

时间:2018-10-16 12:18:57

标签: c# sql sql-server

我已使用String Exec存储过程

ALTER procedure [dbo].[GetZInfo]
@RegID int = 4
AS

DECLARE @DB NVARCHAR(MAX) = '_' + CAST(@RegID AS NVARCHAR(MAX))
DECLARE @exec NVARCHAR(MAX) =' 
select z.ZIndex, z.LastZMade, z.Zrange,z.LastModified 
FROM [Server\SQL].'+@DB+'.dbo.ZEntrytable z 
WHERE z.LastZMade > (DATEADD(HH,6,DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0))) 
ORDER BY z.ZIndex DESC
'

EXEC (@exec)

SQL结果

enter image description here

我想将SQL结果获取到C#comboBox

comboBoxZinfo.Items.Clear();

Int32 RegIDxml;
RegIDxml = Int32.Parse(GetRegIDxml());

SqlCommand cmd = new SqlCommand("GetZInfo", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@RegID", RegIDxml)); //RegID.text
conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
    comboBoxZinfo.Items.Add(dr["ZIndex"].ToString());
}

conn.Close();

它不适用于Exec,但如果我使用简单的select语句,则它可以工作

简单的select语句正在工作:

ALTER procedure [dbo].[GetZInfo]
@RegID int = 4
AS
select gci.FranchisesName from dbo.GetClientInfo gci

1 个答案:

答案 0 :(得分:1)

尝试

da.Fill(dt);
comboBoxZinfo.DataSource = dt;
comboBoxZinfo.DisplayMember = "ZIndex";
comboBoxZinfo.ValueMember = "ZIndex";