Answer the question
In order to leave comments, you need to log in
How to call a stored procedure from an application?
The project code has a method that receives an sql query as input, and displays the contents of the table in the DataGridView as output:
private void GetData(string selectCommand)
{
String connectionString = @"Data Source=WIN-940; Initial Catalog = auto_repair;User ID=test;Password=123";
dataAdapter = new SqlDataAdapter(selectCommand, connectionString);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bindingSource1;
GetData("select * from Vehicles");
}
using (var sqlConn = new SqlConnection("строка соединения"))
{
var sqlCmd = new SqlCommand("Имя Stored Procedure", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
}
Answer the question
In order to leave comments, you need to log in
dataAdapter = new SqlDataAdapter("Имя процедуры", connectionString);
dataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
var connection = new SqlConnection(connectionString);
// connection.Open();
var cmd = new SqlCommand("Имя процедуры", connection);
cmd.CommandType = CommandType.StoredProcedure;
dataAdapter = new SqlDataAdapter(cmd);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question