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

标签 c# sql-server vb.net error-handling

更新:

我试图在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/

相关文章:

c# - List<T> 到 T[] 而不复制

java - Doxygen 注释/属性被忽略

c# - 如何在 C# 中将 className 传递给将泛型类型作为参数的方法

sql - 将 T-SQL 查询转换为 ANSI SQL-99 标准

vb.net - 尝试使用 Catch 显示输入错误消息

vb.net - VisualBasic : casting Exception into ErrObject

c# - 我们可以同时从 CodeExpressions 和文字源代码构造一个程序集吗?

mysql - 缓慢的存储过程 - SQL Server

sql - 在 Azure Web 应用程序中发布 MVC 应用程序

c# - 在句子中查找字符串表达式