gpt4 book ai didi

c# - SQLSERVER和dot.net之间的消息限制错误

转载 作者:行者123 更新时间:2023-12-03 09:01:20 27 4
gpt4 key购买 nike

更新:

我试图在sql-server的本地实例上运行它,可惜它起作用了!!!

现在我知道代码是正确的,我需要找到某种DBA限制(并要求DBA删除)

有什么想法吗?

using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace testDBMessages
{
public class CGeneral
{
// Declare and instantiate connection
private static Form1 caller;

public CGeneral(Form1 caller1)
{
caller = caller1;
string connString = "server=(local)\\SQLEXPRESS;database=tests;Integrated Security=SSPI";

SqlConnection cn = new SqlConnection(connString);

cn.InfoMessage += new SqlInfoMessageEventHandler(CnInfoMessage);
cn.FireInfoMessageEventOnUserErrors = true;
SqlCommand cmd = new SqlCommand();

String sql = "dbo.fillTables";
cmd.Connection = cn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sql;
cmd.Parameters.Add(new SqlParameter("@test", 6));
try
{
cn.Open();
SqlDataReader sdr;
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
cn.Close();
}
}

static void CnInfoMessage(object sender, SqlInfoMessageEventArgs ev)
{
foreach (SqlError err in ev.Errors)
{
Console.WriteLine("Message- " + err.Message);
caller.addMessage(err.Message);
}
}
}
}

表格代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testDBMessages
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
CGeneral a = new CGeneral(this);
}

private void Form1_Load(object sender, EventArgs e)
{
Application.DoEvents();
}

public void addMessage(string msg)
{
listView1.Items.Add(msg);
listView1.Refresh();

}
}
}

存储过程
ALTER PROCEDURE [dbo].[fillTables]
(
@test smallint
)

AS

BEGIN
declare @counter as int
SET @counter=1
while @counter<100
BEGIN
Insert into tests.dbo.tToFill (id,description,testNum)
Values (@counter,'test_1',@test)
RAISERROR ('RECORD NUM %d',10,1,@counter)
SET @counter=@counter+1
END
END
GO

最佳答案

您使用集成安全性连接到数据库的用户是否对存储过程(dbo.filltables)具有EXECUTE权限,因为这表明只有dbo(数据库所有者)对该过程具有完全权限。

您将需要向任何想要使用它的人授予权限。如果出于安全考虑,请谨慎授予每个人权利。

关于c# - SQLSERVER和dot.net之间的消息限制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/919998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com