How Can i display the output of SQL PRINT Command in C#?
You need to subscribe to the SqlConnection.InfoMessage
Event.
MSDN has some example code here.
Capture Stored Procedure print output in .NET
You can do this by adding an event handler to the InfoMessage event on the connection.
myConnection.InfoMessage += new SqlInfoMessageEventHandler(myConnection_InfoMessage);
void myConnection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
Console.WriteLine(e.Message);
}
Need to get the SQL Server PRINT value in C#
You can use the SqlConnection.InfoMessage
event.
How Can i display the output of MySQL “PRINT” Command in C#?
you variable for the procedure is sYear
But in your code you use sAnno
CREATE TABLE mytable_2025 ( id int)
✓
CREATE PROCEDURE `mysp`(IN sYear VARCHAR(255))
BEGIN
DECLARE 2sYear VARCHAR(255);
SET 2sYear = sYear;
SET @s = CONCAT('SELECT * FROM `mytable_',2sYear,'`;');
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
✓
call mysp('2025')
| id |
| -: |
✓
SELECT @s
| @s |
| :---------------------------- |
| SELECT * FROM `mytable_2025`; |
SELECT @2sYear
| @2sYear |
| :------ |
| null |
db<>fiddle here
Print OUT parameter to c# winform
Use ExecuteNonQuery
instead of ExecuteScalar
and retrieve the output parameter value after executing the query:
cmd.Parameters["@ErrorPrint"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
string message = cmd.Parameters["@ErrorPrint"].Value.ToString();
connection.Close();
return message;
Avoid using the sp_
stored procedure name prefix as that denotes system stored procedures. See the documentation.
How do I print SQL query using MessageBox in C#
Add a datagridview control to show multiple rows and try this code and rename it what you want and replace this YourDataGridVeiwName
from the name you set for datagridview control and try below code
private void button1_Click(object sender, EventArgs e)
{
try
{
string connectionString = "Data Source=FAREEDH;Initial Catalog=DeakinsTrade;Integrated Security=True";
string query = "SELECT ProductName FROM Products;";
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, con))
{
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(command))
{
DataTable dt = new DataTable();
sda.Fill(dt);
YourDataGridVeiwName.DataSource= dt;
}
con.Close();
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
Related Topics
String List in SQLcommand Through Parameters in C#
How to Write My C++ Function So I Can Call It from C#
ASP.NET Core MVC - Client-Side Validation for Custom Attribute
C# Null Coalescing Operator Equivalent for C++
Detecting Symbolic Links and Pipes in Mono
How to Convert Style Text to C# Object Such as Class/Hashtable/Collection
Changing a C# Delegate's Calling Convention to Cdecl
Error While Using Executenonquery C#
Android Emulator Not Connecting to Localhost API
Wcf Service Attribute to Log Method Calls and Exceptions
Twoway-Bind View's Dependencyproperty to Viewmodel's Property
Windows Form Application Exception
C# Fill Combo Box from SQL Datatable
C# Call C++ Dll Passing Pointer-To-Pointer Argument
How to Run Sonarqube Code Analysis for .Net Core (C#) on Linux