我正在使用C#解决方案,并且我已经使用DataTable对象填充了我的txtbox,然后检索了行号的信息,问题是,我有导航按钮,下一个和上一个,我只是注意到当我将我的函数调用到PopulateView();按下此按钮后,我重做数据库连接,SQLite查询和数据表填充。
我感到惭愧但是,我真的不知道放置此功能的适当位置在哪里,以便我在此表单中的其他功能接收这些对象。
我的代码:
namespace X_Project{
public partial class Cad_Prod : Form
{
System.Data.SQLite.SQLiteConnection conn1 = new System.Data.SQLite.SQLiteConnection("data source=X.s3db");
int prodstart = 0;
public Cad_Prod()
{
InitializeComponent();
}
public void Cad_Prod_Load(object sender, EventArgs e)
{
}
private void PopulateView()
{
string query = "SELECT * FROM Cad_Prod";
string query2 = "SELECT * FROM Cat";
try
{
using (SQLiteCommand comm = new SQLiteCommand(query, conn1))
using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(comm))
using (SQLiteCommand comm2 = new SQLiteCommand(query2, conn1))
using (SQLiteDataAdapter adapter2 = new SQLiteDataAdapter(comm2))
{
conn1.Open();
DataTable produtos = new DataTable();
adapter.Fill(produtos);
DataTable categoria = new DataTable();
adapter2.Fill(categoria);
int auxCat = int.Parse(produtos.Rows[prodstart].ItemArray[4].ToString()) - 1;
txtProdutoNome.Text = produtos.Rows[prodstart].ItemArray[1].ToString();
txtProdutoPreco.Text = produtos.Rows[prodstart].ItemArray[2].ToString();
txtProdutoQtd.Text = produtos.Rows[prodstart].ItemArray[3].ToString();
cbbCategoria.Text = categoria.Rows[auxCat].ItemArray[1].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnAvancar_Click(object sender, EventArgs e)
{
prodstart = prodstart + 1;
PopulateView();
}
private void btnVoltar_Click(object sender, EventArgs e)
{
prodstart = prodstart - 1;
PopulateView();
}
}
上面的代码有效,但没有我想要的方式,我真正想做的是:
namespace X_Project\
{
public partial class Cad_Prod : Form
{
System.Data.SQLite.SQLiteConnection conn1 = new System.Data.SQLite.SQLiteConnection("data source=X.s3db");
int prodstart = 0;
public Cad_Prod()
{
InitializeComponent();
}
public void Cad_Prod_Load(object sender, EventArgs e)
{
string query = "SELECT * FROM Cad_Prod";
string query2 = "SELECT * FROM Cat";
try
{
using (SQLiteCommand comm = new SQLiteCommand(query, conn1))
using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(comm))
using (SQLiteCommand comm2 = new SQLiteCommand(query2, conn1))
using (SQLiteDataAdapter adapter2 = new SQLiteDataAdapter(comm2))
{
conn1.Open();
DataTable produtos = new DataTable();
adapter.Fill(produtos); // I would like this to be inherited
DataTable categoria = new DataTable();
adapter2.Fill(categoria); // I would like this to be inherited
}
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void PopulateView()
{
int auxCat = int.Parse(produtos.Rows[prodstart].ItemArray[4].ToString()) - 1;
txtProdutoNome.Text = produtos.Rows[prodstart].ItemArray[1].ToString();
txtProdutoPreco.Text = produtos.Rows[prodstart].ItemArray[2].ToString();
txtProdutoQtd.Text = produtos.Rows[prodstart].ItemArray[3].ToString();
cbbCategoria.Text = categoria.Rows[auxCat].ItemArray[1].ToString();
}
private void btnAvancar_Click(object sender, EventArgs e)
{
prodstart = prodstart + 1;
PopulateView();
}
private void btnVoltar_Click(object sender, EventArgs e)
{
prodstart = prodstart - 1;
PopulateView();
}
}
答案 0 :(得分:0)
将dataTable放在使用" Lazy初始化程序"的表单级属性变量中。模式,即它们被编码的位置,因此它们只调用数据库一次,并用第一次的数据表填充静态私有变量;每次连续调用都会读取私有变量并跳过数据库调用。
conftest.py