请告诉此代码的第一,上一个,下一个,最后一个按钮代码.每次我传递"id1"时, (主键)插入sql语句.请帮助我 [英] Pls tell first,prev,next,last buttons code for this code.Every time I am passing "id1" (Primary Key) into the sql statement. pls help me
本文介绍了请告诉此代码的第一,上一个,下一个,最后一个按钮代码.每次我传递"id1"时, (主键)插入sql语句.请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public partial class frmBranchDetails : Form
{
SqlConnection con;
SqlDataAdapter da1;
DataSet ds1;
SqlCommandBuilder cmb;
public static int id1,hr1;
int rno = 0;
frmdgvBranchDetails frm1 = new frmdgvBranchDetails();
public frmBranchDetails()
{
InitializeComponent();
}
private void frmBranchDetails_Load(object sender, EventArgs e)
{
id1 = frmdgvBranchDetails.id;
hr1 = frmdgvBranchDetails.hr;
con = new SqlConnection("user id=sa;password=arun;database=Branch Module");
show_data();
}
private void show_data()
{
da1 = new SqlDataAdapter("Select SNo,BranchName,BranchCode,Address1,Address2,Street,City,State,Country,ZipCode,Phone1,Phone2,Fax,EmailId,Website,Description from BranchDetails where SNo=''" + id1 + "''", con);
da1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
ds1 = new DataSet();
da1.Fill(ds1, "BranchDetails");
if (ds1.Tables[0].Rows.Count > 0)
{
txtBranchName.Text = ds1.Tables[0].Rows[rno][1].ToString();
txtBranchCode.Text = ds1.Tables[0].Rows[rno][2].ToString();
txtAddress1.Text = ds1.Tables[0].Rows[rno][3].ToString();
txtAddress2.Text = ds1.Tables[0].Rows[rno][4].ToString();
txtStreet.Text = ds1.Tables[0].Rows[rno][5].ToString();
txtCity.Text = ds1.Tables[0].Rows[rno][6].ToString();
txtState.Text = ds1.Tables[0].Rows[rno][7].ToString();
cmbCountry.Text = ds1.Tables[0].Rows[rno][8].ToString();
txtZipCode.Text = ds1.Tables[0].Rows[rno][9].ToString();
txtPhone1.Text = ds1.Tables[0].Rows[rno][10].ToString();
txtPhone2.Text = ds1.Tables[0].Rows[rno][11].ToString();
txtFax.Text = ds1.Tables[0].Rows[rno][12].ToString();
txtEmail.Text = ds1.Tables[0].Rows[rno][13].ToString();
txtWebsite.Text = ds1.Tables[0].Rows[rno][14].ToString();
txtDescription.Text = ds1.Tables[0].Rows[rno][15].ToString();
}
}
public void Save_Changes()
{
cmb = new SqlCommandBuilder(da1);
da1.Update(ds1, "BranchDetails");
}
private void show()
{
}
private void btnFirst_Click(object sender, EventArgs e)
{
rno=0;
id1=1;//(record 1)
show_data();
}
private void btnPrevious_Click(object sender, EventArgs e)
{
rno=0;
id1=id1-1;//previous record
show_data();
}
private void btnLast_Click(object sender, EventArgs e)
{
rno = 0;
id1 = 5;//Last record
show_data();
}
private void btnNext_Click(object sender, EventArgs e)
{
rno = 0;
id1 = id1 + 1;//Next record
show_data();
}
private void btnNew_Click(object sender, EventArgs e)
{
id1 = ds1.Tables[0].Rows.Count + 1;
txtBranchName.Text = "";
txtBranchCode.Text = "";
txtAddress1.Text = "";
txtAddress2.Text = "";
txtStreet.Text = "";
txtCity.Text = "";
txtState.Text = "";
cmbCountry.Text = "";
txtZipCode.Text = "";
txtPhone1.Text = "";
txtPhone2.Text = "";
txtFax.Text = "";
txtEmail.Text = "";
txtWebsite.Text = "";
txtDescription.Text = "";
Save_Changes();
}
private void btnDelete_Click(object sender, EventArgs e)
{
rno = 0;
ds1.Tables[0].Rows[rno].Delete();
MessageBox.Show("Record Deleted");
Save_Changes();
btnNext.PerformClick();
}
private void btnSave_Click(object sender, EventArgs e)
{
if (id1 == Convert.ToInt32(ds1.Tables[0].Rows.Count + 1))
{
DataRow dv = ds1.Tables[0].NewRow();
dv[1] = txtBranchName.Text;
dv[2] = txtBranchCode.Text;
dv[3] = txtAddress1.Text;
dv[4] = txtAddress2.Text;
dv[5] = txtStreet.Text;
dv[6] = txtCity.Text;
dv[7] = txtState.Text;
dv[8] = cmbCountry.Text;
dv[9] = txtZipCode.Text;
dv[10] = txtPhone1.Text;
dv[11] = txtPhone2.Text;
dv[12] = txtFax.Text;
dv[13] = txtEmail.Text;
dv[14] = txtWebsite.Text;
dv[15] = txtDescription.Text;
ds1.Tables[0].Rows.Add(dv);
Save_Changes();
MessageBox.Show("Record(s) Added");
}
else
{
id1 = id1;
ds1.Tables[0].Rows[rno][1] = txtBranchName.Text;
ds1.Tables[0].Rows[rno][2] = txtBranchCode.Text;
ds1.Tables[0].Rows[rno][3] = txtAddress1.Text;
ds1.Tables[0].Rows[rno][4] = txtAddress2.Text;
ds1.Tables[0].Rows[rno][5] = txtStreet.Text;
ds1.Tables[0].Rows[rno][6] = txtCity.Text;
ds1.Tables[0].Rows[rno][7] = txtState.Text;
ds1.Tables[0].Rows[rno][8] = cmbCountry.Text;
ds1.Tables[0].Rows[rno][9] = txtZipCode.Text;
ds1.Tables[0].Rows[rno][10] = txtPhone1.Text;
ds1.Tables[0].Rows[rno][11] = txtPhone2.Text;
ds1.Tables[0].Rows[rno][12] = txtFax.Text;
ds1.Tables[0].Rows[rno][13] = txtEmail.Text;
ds1.Tables[0].Rows[rno][14] = txtWebsite.Text;
ds1.Tables[0].Rows[rno][15] = txtDescription.Text;
Save_Changes();
MessageBox.Show("Record(s) updated");
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
txtBranchName.Text = "";
txtBranchCode.Text = "";
txtAddress1.Text = "";
txtAddress2.Text = "";
txtStreet.Text = "";
txtCity.Text = "";
txtState.Text = "";
cmbCountry.Text = "";
txtZipCode.Text = "";
txtPhone1.Text = "";
txtPhone2.Text = "";
txtFax.Text = "";
txtEmail.Text = "";
txtWebsite.Text = "";
txtDescription.Text = "";
}
}
}
推荐答案
con = new SqlConnection("user id=sa;password=arun;database=Branch Module");
永远不要使用SA帐户进行数据访问.
Never, ever use the SA account for data access.
da1 = new SqlDataAdapter("Select SNo,BranchName,BranchCode,Address1,Address2,Street,City,State,Country,ZipCode,Phone1,Phone2,Fax,EmailId,Website,Description from BranchDetails where SNo=''" + id1 + "''", con);
切勿使用字符串串联来构成查询.使用参数化查询或更好的存储过程.
并学习如何使用DataBinding
Never use string concatenation to form a query. Use parameterized queries, or better yet, stored procedures.
And learn how to use DataBinding
这篇关于请告诉此代码的第一,上一个,下一个,最后一个按钮代码.每次我传递"id1"时, (主键)插入sql语句.请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文