您好,感谢您的阅读。
我有一个包含唯一ID和电子邮件的数据库。当我点击一个按钮时,我想从数据库中获取所有电子邮件,并在我的文本框中显示ID为Emailliste。
如果有效,它应该在文本框中列出所有电子邮件,并在它们之间。像这样。 Email1 @ email.com,Email2 @email.com
这是我的C#代码:
using (SqlConnection connection = new SqlConnection("Data Source=PCM13812;Initial Catalog=Newsletter;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "GetAllEmail";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = connection;
connection.Open();
string email = (string)cmd.ExecuteScalar();
EmailListe.Text = email;
connection.Close();
}
这是我创建存储过程的脚本:
CREATE PROCEDURE [dbo].[GetAllEmail]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Email nvarchar(50)
SELECT @Email = COALESCE(@Email+ ', ', '') + Email
FROM Newsletter
END
我不确定我做错了什么,但我希望有人可以帮助我
答案 0 :(得分:1)
您需要从程序中选择@Email
CREATE PROCEDURE [dbo].[GetAllEmail]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Email nvarchar(50)
SELECT @Email = COALESCE(@Email+ ', ', '') + Email
FROM Newsletter
-- You need do this
SELECT @Email
END
答案 1 :(得分:0)
您的存储过程(如果我们正在谈论MS SQL)实际上并没有返回结果,您缺少输出SELECT。使用当前过程,您只需为@Email变量赋值。
CREATE PROCEDURE [dbo].[GetAllEmail]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Email nvarchar(50)
SELECT @Email = COALESCE(@Email+ ', ', '') + Email
FROM Newsletter
SELECT @Email --// this is missing, so the proc will actually return a result
END