我用的是sql server 2000,我用短点测试过了,是在调用窗体的代码即前一页面supplier_select myform = new supplier_select(); myform.showdialog(); 的myform.showdialog()这一句运行得特别慢,不知道是什么原因。
to 这个不是主窗体,我再问一下我打开每个窗体都要重新连接数据库吗?因为我基本上每个窗体都要连接到数据库的,如果能只用一次数据库连接就能在所有的窗体上用就可以缩短你说的数据库连接的时间问题了,请问有没有好的方法?你在每个窗体中都传进去connectionstring,那么操作数据库的时候都是新创建一个connection,那么是很慢的,如果你已经有connection了,那么操作数据库的时候,用已有的connection来操作就行了。
to 请问如何在每个窗体都调用conn连接而不重新连接数据库?方法一,做成全局变量,例如: public class PublicMember { private PublicMember(); public static SqlConnection myConn; }// Get connection use "PublicMember.myConn"方法二,做成主窗体的局部成员,然后创建子窗体的时候,把局部成员传进去,例如: public yourchildForm:.. { public yourchildForm( ref SqlConnection myConn ) { //Use "myConn" to init your class member }
第一次连接速度慢 绑定combobox算法优化
我用的填充DATASET的代码,请帮我看看该如何改进,在主窗口已经定义了CONN,不过不知道该怎样对DATASET,DATAADAPTER进行优化。 private void bind() { string strSQL1; strSQL1 = "SELECT SECTIONID,SECTIONID+' ' +NAME AS SN FROM t_b_SECTION ORDER BY SECTIONID ASC"; SqlDataAdapter da1 = new SqlDataAdapter(strSQL1, source); DataSet ds1 = new DataSet(); da1.Fill(ds1,"section"); SECTIONID.DataSource = ds1 ; SECTIONID.DisplayMember = "section.SN"; SECTIONID.ValueMember = "section.SECTIONID"; SECTIONID.SelectedIndex = -1;
string strSQL2; strSQL2 = "SELECT SUP_NO,STR(SUP_NO) + ' ' +COMPANY_NAME AS SC FROM t_b_SUPPLIER WHERE STATUS!=9 ORDER BY SUP_NO ASC"; SqlDataAdapter da2 = new SqlDataAdapter(strSQL2, source); DataSet ds2 = new DataSet(); da2.Fill(ds2,"supplier"); SUP_NO.DataSource = ds2 ; SUP_NO.DisplayMember = "supplier.SC"; SUP_NO.ValueMember = "supplier.SUP_NO"; SUP_NO.SelectedIndex = -1; SUP_TEST.DataSource = ds2 ; SUP_TEST.DisplayMember = "supplier.SUP_NO"; SUP_TEST.ValueMember = "supplier.SUP_NO"; SUP_TEST.SelectedIndex = -1;
You should read detail info about "SqlDataAdapter" constructor in MSDN.Sample code as follows://SqlConnection sqlConn; SqlDataAdapter da1 = new SqlDataAdapter(strSQL1, sqlConn);
建议使用 MS Enterprise Library 微软企业库,给我们创建应用程序提供一种最佳实践,不妨有时间看看:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/consyscourse/EntLib.aspx
Knight94(愚翁) ( ) 信誉:110 2006-5-10 12:19:30 得分: 0
to 请问如何在每个窗体都调用conn连接而不重新连接数据库?方法一,做成全局变量,例如: public class PublicMember { private PublicMember(); public static SqlConnection myConn; }// Get connection use "PublicMember.myConn"方法二,做成主窗体的局部成员,然后创建子窗体的时候,把局部成员传进去,例如: public yourchildForm:.. { public yourchildForm( ref SqlConnection myConn ) { //Use "myConn" to init your class member }
代码一部分如下:
protected override bool ProcessDialogKey(System.Windows.Forms.Keys aKey)
{
if (aKey == Keys.Enter)
{
if(this.SUP_SELECTED.Focused)
{
int index = SUP_TEST.FindString(SUP_SELECTED.Text.Trim());
SUP_SELECTED.SelectedIndex = index;
button1.Focus();
}
}
base.ProcessDialogKey(aKey);
return false;
} private void supbind()
{
string strConn="server=137.200.32.15;"+"UID=sa;pwd='';"+"database=ALFA_V2";
string strSQL2;
strSQL2 = "SELECT SUP_NO,STR(SUP_NO) + ' ' +COMPANY_NAME AS SC FROM t_b_SUPPLIER WHERE STATUS!=9 ORDER BY SUP_NO ASC";
SqlDataAdapter da2 = new SqlDataAdapter(strSQL2, strConn);
DataSet ds2 = new DataSet();
da2.Fill(ds2,"supplier");
SUP_SELECTED.DataSource = ds2 ;
SUP_SELECTED.DisplayMember = "supplier.SC";
SUP_SELECTED.ValueMember = "supplier.SUP_NO";
SUP_SELECTED.SelectedIndex = -1; SUP_TEST.DataSource = ds2 ;
SUP_TEST.DisplayMember = "supplier.SUP_NO";
SUP_TEST.ValueMember = "supplier.SUP_NO";
SUP_TEST.SelectedIndex = -1;
}
private void supplier_select_Load(object sender, System.EventArgs e)
{
supbind();
} private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
} private void button1_Click(object sender, System.EventArgs e)
{
SUP_NO = SUP_SELECTED.SelectedValue.ToString(); if(SUP_SELECTED.SelectedIndex != -1)
{
contract_sp_new myform =new contract_sp_new();
myform.Show();
//this.Hide();
}
else
{
MessageBox.Show("please input valid supplier");
return;
}
}
myform.showdialog();
的myform.showdialog()这一句运行得特别慢,不知道是什么原因。
SUP_SELECTED.DisplayMember = "supplier.SC";
如果不是主窗体的话,如果其他地方已经连接数据库了,那么就不要重新连接了,用已有的连接即可。
在主窗體上把sqlconnection , sqlDataAdapter , dataset 等都弄好後,在form2 中(假設你的第二個窗體叫form2), 不用再出現sqlconnection和sqlDataAdapter了,隻要從工具箱中拖出一個dataset控件後,指定數據集就可以了,然後你就可以用這個datagrid來綁定這個dataset了,明白?
{
Application.Run(new login());
}
private void button1_Click(object sender, System.EventArgs e)
{
user_id = USER_ID.Text;
if(user_id == "")
{
MessageBox.Show("please input USER_ID");
return;
} string user_pwd = USER_PWD.Text;
if(user_pwd == "")
{
MessageBox.Show("please input password");
return;
}
string strSQL;
strSQL = "SELECT * FROM A2_USER WHERE USER_ID = '" + user_id + "'";
SqlDataAdapter da = new SqlDataAdapter(strSQL, source);
DataSet ds = new DataSet();
da.Fill(ds,"USER");
try
{
if(ds.Tables["USER"].Rows.Count != 0)
{
if(ds.Tables["USER"].Rows[0]["USER_PWD"].ToString() == user_pwd.ToString())
{
this.Hide();
contract myForm = new contract();
myForm.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("password is wrong,please enter correct password");
return;
}
}
else
{
MessageBox.Show("Not exist this user");
return;
}
}
catch
{
MessageBox.Show("exception");
}
finally
{
//this.Hide();
//this.Close();
//this.Visible = false;
}
}
internal static string user_id = user_id ;
private void login_Load(object sender, System.EventArgs e)
{
USER_PWD.PasswordChar = '*'; foreach(Control ctrl in this.Controls)
{
if ((ctrl is TextBox) || (ctrl is ComboBox)) //如果文本框或下拉框放在组合框中,你应该用如groupBox1.TextBox
{
ctrl.KeyDown += new System.Windows.Forms.KeyEventHandler(this.EnterKeyDown);
}
} } private void EnterKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
System.Windows.Forms.SendKeys.Send("{TAB}");
}
} private void button2_Click(object sender, System.EventArgs e)
{
this.Close();
}请问如何在每个窗体都调用conn连接而不重新连接数据库?
所以你隻要第一次對數據庫連接成功後,把相關的數據通過sqlDataAdapter傳到Dataset後,就可以對dataset來操作了。隻有當你需要把數據回寫到數據庫時,再用dataset和數據庫通信,否則你的所有本地操作都是對dataset的操作,所以不用每個窗體都和數據庫連,隻要每個窗體都擁有一個dataset就行了,注意每個dataset都要指定和sql打交道的通道,不過這一步很容易,當你把dataset控件放入窗體時,就會自動提示你做這一步的。自己試試,我要吃飯去了。以前我也是每個窗體都connection,這樣做太慢了。也不好.
public class PublicMember
{
private PublicMember();
public static SqlConnection myConn;
}// Get connection use "PublicMember.myConn"方法二,做成主窗体的局部成员,然后创建子窗体的时候,把局部成员传进去,例如:
public yourchildForm:..
{
public yourchildForm( ref SqlConnection myConn )
{
//Use "myConn" to init your class member
}
绑定combobox算法优化
private void bind()
{
string strSQL1;
strSQL1 = "SELECT SECTIONID,SECTIONID+' ' +NAME AS SN FROM t_b_SECTION ORDER BY SECTIONID ASC";
SqlDataAdapter da1 = new SqlDataAdapter(strSQL1, source);
DataSet ds1 = new DataSet();
da1.Fill(ds1,"section");
SECTIONID.DataSource = ds1 ;
SECTIONID.DisplayMember = "section.SN";
SECTIONID.ValueMember = "section.SECTIONID";
SECTIONID.SelectedIndex = -1;
string strSQL2;
strSQL2 = "SELECT SUP_NO,STR(SUP_NO) + ' ' +COMPANY_NAME AS SC FROM t_b_SUPPLIER WHERE STATUS!=9 ORDER BY SUP_NO ASC";
SqlDataAdapter da2 = new SqlDataAdapter(strSQL2, source);
DataSet ds2 = new DataSet();
da2.Fill(ds2,"supplier");
SUP_NO.DataSource = ds2 ;
SUP_NO.DisplayMember = "supplier.SC";
SUP_NO.ValueMember = "supplier.SUP_NO";
SUP_NO.SelectedIndex = -1; SUP_TEST.DataSource = ds2 ;
SUP_TEST.DisplayMember = "supplier.SUP_NO";
SUP_TEST.ValueMember = "supplier.SUP_NO";
SUP_TEST.SelectedIndex = -1;
string strSQL3;
strSQL3 = "SELECT LP_ID FROM A2_LP_CONTRACT";
SqlDataAdapter da3 = new SqlDataAdapter(strSQL3, source);
DataSet ds3 = new DataSet();
da3.Fill(ds3,"lp");
LP_ID.DataSource = ds3 ;
LP_ID.DisplayMember = "lp.LP_ID";
LP_ID.ValueMember = "lp.LP_ID";
LP_ID.SelectedIndex = -1; }
SqlDataAdapter da1 = new SqlDataAdapter(strSQL1, sqlConn);
to 请问如何在每个窗体都调用conn连接而不重新连接数据库?方法一,做成全局变量,例如:
public class PublicMember
{
private PublicMember();
public static SqlConnection myConn;
}// Get connection use "PublicMember.myConn"方法二,做成主窗体的局部成员,然后创建子窗体的时候,把局部成员传进去,例如:
public yourchildForm:..
{
public yourchildForm( ref SqlConnection myConn )
{
//Use "myConn" to init your class member
}
同意这种方法.或者使用MS的那个类库.