如何在消息框中显示sql查询打印的值

发布于 2024-08-17 21:56:54 字数 887 浏览 6 评论 0原文

我想打印 SQL Server 返回的值。

If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama') 
   BEGIN   
    INSERT INTO ItemList (ItemName) VALUES('txtItemNamea')  
   END 
ELSE  
   BEGIN 
    Print 'Duplicate' 
   END

此查询将返回受影响的行数或 Duplicate

我想在 C# 的 MessageBox.Show() 中使用此 Duplicate

string query1 = "If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemName') BEGIN  INSERT INTO ItemList (ItemName) VALUES('txtItemName')  END ELSE  BEGIN Print 'Duplicate' END";
            SqlCommand cmd = new SqlCommand(query1, conn);
            SqlDataReader dr;
            conn.Open();
            dr=cmd.ExecuteReader();
            conn.Close();
MessageBox.Show(dr);

我不知道如何使用 < code>dr 来执行此操作。请帮我在这里打印副本

MessageBox.Show(dr);

我需要在这里做什么?

I want to print a value that is returned by SQL Server.

If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama') 
   BEGIN   
    INSERT INTO ItemList (ItemName) VALUES('txtItemNamea')  
   END 
ELSE  
   BEGIN 
    Print 'Duplicate' 
   END

This query will either return me either number of rows affected or Duplicate

I want to use this Duplicate in C# in MessageBox.Show()

string query1 = "If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemName') BEGIN  INSERT INTO ItemList (ItemName) VALUES('txtItemName')  END ELSE  BEGIN Print 'Duplicate' END";
            SqlCommand cmd = new SqlCommand(query1, conn);
            SqlDataReader dr;
            conn.Open();
            dr=cmd.ExecuteReader();
            conn.Close();
MessageBox.Show(dr);

I don't know how to use dr to do this. Please help me out to print Duplicate here

MessageBox.Show(dr);

What do I need to do here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-08-24 21:56:54

晚了几年,但您应该能够通过将事件处理程序附加到 SqlConnection 对象上的 InfoMessage 事件 -

但只有在无法(例如出于某种原因) 使用此线程中提到的替代方案。

static void Main(string[] args)
{
    using (SqlConnection connection = new SqlConnection(@"someconnectionstring"))
    {
        connection.Open();
        using(SqlCommand command = new SqlCommand("test", connection))
        {
            connection.InfoMessage += new SqlInfoMessageEventHandler(connection_InfoMessage);
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt); // Do something with DataTable
            }
        }
    }
}

static void connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
    // e contains info message etc
}

a few years late but you should be able to retrieve print/info text (like originally asked) by attaching an eventhandler to the InfoMessage event on the SqlConnection object -

But only do this if you can't (for some reason) use the alternatives mentioned in this thread.

static void Main(string[] args)
{
    using (SqlConnection connection = new SqlConnection(@"someconnectionstring"))
    {
        connection.Open();
        using(SqlCommand command = new SqlCommand("test", connection))
        {
            connection.InfoMessage += new SqlInfoMessageEventHandler(connection_InfoMessage);
            using (SqlDataAdapter adapter = new SqlDataAdapter(command))
            {
                DataTable dt = new DataTable();
                adapter.Fill(dt); // Do something with DataTable
            }
        }
    }
}

static void connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
    // e contains info message etc
}
蓝色星空 2024-08-24 21:56:54

使用 ADO.NET,有四个选项可用于从 SQL 查询返回信息:

  • 使用 DataSet 对象来收集返回的行,并除了返回值和返回参数之外还可以处理这些行。
  • 使用 DataReader 对象收集返回的行、在这些行中移动以及收集返回值和返回参数。
  • 使用 ExecuteScalar 方法返回结果第一行第一列的值以及返回值和返回参数。这对于聚合函数最有用。
  • 使用 ExecuteNonQuery 方法仅返回返回参数和返回值。任何返回的行都将被丢弃。这对于执行操作查询最有用。

这些都是从命令对象调用的方法。

给猫剥皮有很多不同的方法,您可以使用输出参数,可以使用 ExecuteScalar,可以使用返回值,也可以使用虚拟记录集。

您应该能够在 C# 中执行类似以下操作来从查询中获取返回值。

// add a new parameter, with any name we want - its for our own use only 
SqlParameter sqlParam = com.Parameters.Add("@ReturnValue", SqlDbType.Int); 
// set the direction flag so that it will be filled with the return value 
myParm.Direction = ParameterDirection.ReturnValue;

上面的代码捕获您可以根据需要设置的返回值,可能使用 0 表示存在,使用 1 表示不存在。

If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama')    
   BEGIN      
    INSERT INTO ItemList (ItemName) VALUES('txtItemNamea') 
    Return 0;    
   END    
ELSE     
   BEGIN    
    Return 1;    
   END 

然而,上述建议有一点需要注意 - 我通常要么使用像 Linq-to-SQL 或 NHibernate 这样的 ORM,要么使用存储过程。我发现内联 SQL 相当麻烦。因此,虽然我的答案总体上应该是合理的,但您可能需要处理一些细节才能使其完全按照您的需要工作。

Using ADO.NET there are four options for returning information from an SQL query:

  • Use a DataSet object to gather the returned rows and to work with these rows in addition to the return values and the return parameters.
  • Use a DataReader object to gather the returned rows, to move through these rows, and to gather return values and return parameters.
  • Use the ExecuteScalar method to return the value from the first column of the results' first row with the return values and the return parameters. This is most useful with aggregate functions.
  • Use the ExecuteNonQuery method to return only the return parameters and the return values. Any returned rows are discarded. This is most useful for executing action queries.

These are all methods called from your command object.

There are lots of different ways to skin a cat, you can use output parameters, you can use ExecuteScalar, you can use return values or you can use dummy recordsets.

You should be able to something like the following in your C# to get the return value from a query

// add a new parameter, with any name we want - its for our own use only 
SqlParameter sqlParam = com.Parameters.Add("@ReturnValue", SqlDbType.Int); 
// set the direction flag so that it will be filled with the return value 
myParm.Direction = ParameterDirection.ReturnValue;

The code above the captures the return value that you can set as you need, perhaps with a 0 for exists and a 1 for not exists.

If NOT Exists(SELECT * FROM ItemList WHERE ItemName='txtItemNama')    
   BEGIN      
    INSERT INTO ItemList (ItemName) VALUES('txtItemNamea') 
    Return 0;    
   END    
ELSE     
   BEGIN    
    Return 1;    
   END 

There is a bit caveat with the above advice however - I usually either use an ORM like Linq-to-SQL or NHibernate, or I use Stored Procedures. I find inline SQL quite cumbersome. So, while my answer should be sound in general, you will probably need to work through some details to get it working exactly as you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文