Voglio riempire un controllo DataGridView utilizzando DataAdapter. Ma non so come farlo poiché sto usando una stored procedure con parametro. Qualcuno può citare un esempio per favore?Come utilizzare un DataAdapter con procedura memorizzata e il parametro
risposta
l'ho presa ... hehe
protected DataTable RetrieveEmployeeSubInfo(string employeeNo)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
cmd = new SqlCommand("RETRIEVE_EMPLOYEE", pl.ConnOpen());
cmd.Parameters.Add(new SqlParameter("@EMPLOYEENO", employeeNo));
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
cmd.Dispose();
pl.MySQLConn.Close();
}
return dt;
}
+1 Per farlo funzionare. – Garett
Un approccio più pulito sarebbe quello di racchiudere le risorse IDisposable in una clausola using invece di un try finally – cweston
^uso molto più pulito 'Using' block – IteratioN7T
Forse il codice manca questa linea dall'esempio Microsoft:
MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
SqlConnection con = new SqlConnection(@"Some Connection String");
SqlDataAdapter da = new SqlDataAdapter("ParaEmp_Select",con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
+1 per una buona soluzione –
Ci siamo,
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con; //database connection
cmd.CommandText = "WRITE_STORED_PROC_NAME"; // Stored procedure name
cmd.CommandType = CommandType.StoredProcedure; // set it to stored proc
//add parameter if necessary
cmd.Parameters.Add("@userId", SqlDbType.Int).Value = courseid;
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(ds, "Course");
return ds;
SqlConnection con = new SqlConnection(@"Some Connection String");//connection object
SqlDataAdapter da = new SqlDataAdapter("ParaEmp_Select",con);//SqlDataAdapter class object
da.SelectCommand.CommandType = CommandType.StoredProcedure; //command sype
da.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123; //pass perametter
DataTable dt = new DataTable(); //dataset class object
da.Fill(dt); //call the stored producer
public DataSet Myfunction(string Myparameter)
{
config.cmd.Connection = config.cnx;
config.cmd.CommandText = "ProcName";
config.cmd.CommandType = CommandType.StoredProcedure;
config.cmd.Parameters.Add("parameter", SqlDbType.VarChar, 10);
config.cmd.Parameters["parameter"].Value = Myparameter;
config.dRadio = new SqlDataAdapter(config.cmd);
config.dRadio.Fill(config.ds,"Table");
return config.ds;
}
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = <sql server name>;
builder.UserID = <user id>; //User id used to login into SQL
builder.Password = <password>; //password used to login into SQL
builder.InitialCatalog = <database name>; //Name of Database
DataTable orderTable = new DataTable();
//<sp name> stored procedute name which you want to exceute
using (var con = new SqlConnection(builder.ConnectionString))
using (SqlCommand cmd = new SqlCommand(<sp name>, con))
using (var da = new SqlDataAdapter(cmd))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
//Data adapter(da) fills the data retuned from stored procedure
//into orderTable
da.Fill(orderTable);
}
- 1. Chiamare la procedura memorizzata con il parametro Out utilizzando PDO
- 2. Procedura memorizzata con nome tabella come parametro tra gli altri
- 3. Valore restituito procedura memorizzata PDO
- 4. Procedura memorizzata MySQL con elenco argomenti variabile
- 5. Chiamare una procedura memorizzata con asp.net
- 6. Procedura memorizzata più eseguita?
- 7. Procedura memorizzata IF/ELSE
- 8. Cambia il definer della procedura memorizzata
- 9. Procedura memorizzata chiamata con un valore di parametro null con EntityFramework
- 10. mysql DICHIARARE MENTRE fuori procedura memorizzata come?
- 11. Come posso rendere una procedura memorizzata restituire un "set di dati" utilizzando un parametro I pass?
- 12. Selezionare e aggiornare in una procedura memorizzata
- 13. Test schema e procedura memorizzata utilizzando Nunit
- 14. Come posso modificare una procedura memorizzata?
- 15. Chiamare una procedura memorizzata python
- 16. Come chiamare la procedura memorizzata da sospensione?
- 17. Come sopprimere l'output della procedura memorizzata mysql?
- 18. Passa array in una procedura memorizzata
- 19. Come chiamare una procedura memorizzata MySQL da un codice PHP?
- 20. Come creare e utilizzare la tabella temporanea nella procedura memorizzata Oracle?
- 21. Domanda ADO.NET: Quando utilizzare DataReader, DataAdapter
- 22. procedura memorizzata chiamata oracle all'interno select
- 23. Misurare accuratamente le prestazioni della procedura memorizzata
- 24. È possibile avere un parametro predefinito per una procedura memorizzata mysql?
- 25. Procedura memorizzata EXEC vs sp_executesql differenza?
- 26. La procedura memorizzata MySQL sta causando problemi?
- 27. Chiamata alla procedura memorizzata con un ISNULL nel compito. Sintassi non valida?
- 28. Creazione di una procedura memorizzata tramite C#
- 29. Entity Framework - valore restituito della procedura memorizzata
- 30. aggiunta di una colonna nella procedura memorizzata
Ecco un Microsoft [articolo] (http://support.microsoft.com/kb/306574) che fornisce un esempio di ciò. – Garett