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

时间:2009-05-28 09:33:38

标签: 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

2 个答案:

答案 0 :(得分:0)

严重性为10的RAISERROR被归类为警告,因此不会流向客户端代码。

使用16,which is defined as "Indicates general errors that can be corrected by the user" (编辑)我确信过去不同......

RAISERROR ('RECORD NUM %d',16,1,@counter)

答案 1 :(得分:0)

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

您需要为想要使用它的任何人授予权限。如果担心安全问题,请小心授予每个人权利。