C# 从SQL Server 2008刷新组合框
我从C#开始,遇到了一些问题 我想知道在第二个windows窗体(agregar_en_directorio)中保存数据时如何刷新数据 并希望在第一个windows窗体(generar_tarjeta)的组合框中显示新数据 Conexion:conectaraBD.csC# 从SQL Server 2008刷新组合框,c#,sql-server-2008,C#,Sql Server 2008,我从C#开始,遇到了一些问题 我想知道在第二个windows窗体(agregar_en_directorio)中保存数据时如何刷新数据 并希望在第一个windows窗体(generar_tarjeta)的组合框中显示新数据 Conexion:conectaraBD.cs public static SqlConnection ObtenerCOnexion() { SqlConnection Conn = new SqlConnection(@"Data sou
public static SqlConnection ObtenerCOnexion()
{
SqlConnection Conn = new SqlConnection(@"Data source=MY-PC\SQLEXPRESS; Initial Catalog=myDatabase; User Id=user; Password=xxxx");
Conn.Open();
return Conn;
}
组合:
public void fillCombo()
{
string SQL = "select id_persona as identificador, clave_de_identificacion +' '+clave_de_la_dependencia +' '+grado_o_titulo+' '+nombre+' '+ ap+' '+ am DetallesCompletos from directorio";
DataTable dt = new DataTable();
using (SqlConnection Conn2 = conectaraBD.ObtenerCOnexion())
{
using (var cmd = new SqlCommand(SQL, Conn2))
{
try
{
dt.Load(cmd.ExecuteReader());
}
catch (SqlException e)
{
MessageBox.Show("Error al Cargar los Datos" + e.ToString(), "Error SQL",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
comboDe.DataSource = dt;
comboDe.ValueMember = "identificador";
comboDe.DisplayMember = "DetallesCompletos";
}
public static DataTable dataFortheCombos()
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(@"Data source=SAMANIEGO-PC\SQLEXPRESS; Initial Catalog=banco_de_datos; User Id=user; Password=xxx");/
string query = "select id_person as identifier, identification_key +' '+dependence_key +' '+degree_or_title+' '+name+' '+ ap+' '+ am as completedetails from directory";
SqlCommand cmd = new SqlCommand(query, connection);
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(dt);
return dt;
}
public static AutoCompleteStringCollection autocompleteCombos()
{
DataTable dt = dataFortheCombos();
AutoCompleteStringCollection coleccion = new AutoCompleteStringCollection();
foreach (DataRow row in dt.Rows)
{
coleccion.Add(Convert.ToString(row["completedetails"]));
}
return coleccion;
}
public void fillCombos()
{
comboFrom.DataSource = dataFortheCombos();
comboFrom.DisplayMember = "completedetails"; //This is the value shown on the combo for the user
comboFrom.ValueMember = "identifier"; // The selectedc value insert as identifier (is a number)
comboFrom.SelectedIndex = -1; //Clear the combo
//NOTE -> The others combos (urned over and sender) using the same data
}
注意:组合框使用的代码如下(3组合框使用的类似代码)
并将帮助我了解您对GUI的看法我解决了,但我不得不改变一些事情:
public void fillCombo()
{
string SQL = "select id_persona as identificador, clave_de_identificacion +' '+clave_de_la_dependencia +' '+grado_o_titulo+' '+nombre+' '+ ap+' '+ am DetallesCompletos from directorio";
DataTable dt = new DataTable();
using (SqlConnection Conn2 = conectaraBD.ObtenerCOnexion())
{
using (var cmd = new SqlCommand(SQL, Conn2))
{
try
{
dt.Load(cmd.ExecuteReader());
}
catch (SqlException e)
{
MessageBox.Show("Error al Cargar los Datos" + e.ToString(), "Error SQL",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
comboDe.DataSource = dt;
comboDe.ValueMember = "identificador";
comboDe.DisplayMember = "DetallesCompletos";
}
public static DataTable dataFortheCombos()
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(@"Data source=SAMANIEGO-PC\SQLEXPRESS; Initial Catalog=banco_de_datos; User Id=user; Password=xxx");/
string query = "select id_person as identifier, identification_key +' '+dependence_key +' '+degree_or_title+' '+name+' '+ ap+' '+ am as completedetails from directory";
SqlCommand cmd = new SqlCommand(query, connection);
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(dt);
return dt;
}
public static AutoCompleteStringCollection autocompleteCombos()
{
DataTable dt = dataFortheCombos();
AutoCompleteStringCollection coleccion = new AutoCompleteStringCollection();
foreach (DataRow row in dt.Rows)
{
coleccion.Add(Convert.ToString(row["completedetails"]));
}
return coleccion;
}
public void fillCombos()
{
comboFrom.DataSource = dataFortheCombos();
comboFrom.DisplayMember = "completedetails"; //This is the value shown on the combo for the user
comboFrom.ValueMember = "identifier"; // The selectedc value insert as identifier (is a number)
comboFrom.SelectedIndex = -1; //Clear the combo
//NOTE -> The others combos (urned over and sender) using the same data
}
当用户位于combobox comboFrom中时,事件onfocus用于调用刷新
private void comboDe_Enter(object sender, EventArgs e)
{
comboFrom.DataSource = null //Clear the Combo Box
//NOTE -> The others combos (urned over and sender) using the same data
fillCombos();
}
我解决了,但我不得不改变一些事情:
public void fillCombo()
{
string SQL = "select id_persona as identificador, clave_de_identificacion +' '+clave_de_la_dependencia +' '+grado_o_titulo+' '+nombre+' '+ ap+' '+ am DetallesCompletos from directorio";
DataTable dt = new DataTable();
using (SqlConnection Conn2 = conectaraBD.ObtenerCOnexion())
{
using (var cmd = new SqlCommand(SQL, Conn2))
{
try
{
dt.Load(cmd.ExecuteReader());
}
catch (SqlException e)
{
MessageBox.Show("Error al Cargar los Datos" + e.ToString(), "Error SQL",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
comboDe.DataSource = dt;
comboDe.ValueMember = "identificador";
comboDe.DisplayMember = "DetallesCompletos";
}
public static DataTable dataFortheCombos()
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(@"Data source=SAMANIEGO-PC\SQLEXPRESS; Initial Catalog=banco_de_datos; User Id=user; Password=xxx");/
string query = "select id_person as identifier, identification_key +' '+dependence_key +' '+degree_or_title+' '+name+' '+ ap+' '+ am as completedetails from directory";
SqlCommand cmd = new SqlCommand(query, connection);
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(dt);
return dt;
}
public static AutoCompleteStringCollection autocompleteCombos()
{
DataTable dt = dataFortheCombos();
AutoCompleteStringCollection coleccion = new AutoCompleteStringCollection();
foreach (DataRow row in dt.Rows)
{
coleccion.Add(Convert.ToString(row["completedetails"]));
}
return coleccion;
}
public void fillCombos()
{
comboFrom.DataSource = dataFortheCombos();
comboFrom.DisplayMember = "completedetails"; //This is the value shown on the combo for the user
comboFrom.ValueMember = "identifier"; // The selectedc value insert as identifier (is a number)
comboFrom.SelectedIndex = -1; //Clear the combo
//NOTE -> The others combos (urned over and sender) using the same data
}
当用户位于combobox comboFrom中时,事件onfocus用于调用刷新
private void comboDe_Enter(object sender, EventArgs e)
{
comboFrom.DataSource = null //Clear the Combo Box
//NOTE -> The others combos (urned over and sender) using the same data
fillCombos();
}
与论坛网站不同,我们不使用“感谢”或“感谢任何帮助”或签名。请参见“.与论坛网站不同,我们不使用“谢谢”或“感谢任何帮助”或签名。请参见“.仅供参考,您的
SqlConnection
、SqlCommand
和SqlDataAdapter
都需要在中使用块。非常感谢您的评论,考虑到这一点,我仍在改进代码。仅供参考,您的SqlConnection
、SqlCommand
和SqlDataAdapter
都需要使用块进入。非常感谢您的评论,考虑到这一点,我仍在改进代码。