我的代码是否有问题,我是编码错误的初学者:
(超时已过期。在操作或服务器完成之前已超时)
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection SqlConnection(@"Data Source=DESKTOP-J7D5POF;Initial Catalog=ilswork;Persist Security Info=True;User ID=test;Password=*****;connect timeout=9000");
public SqlConnection mycon = new SqlConnection(@"Data Source=DESKTOP-J7D5POF;Initial Catalog=ilswork;Persist Security Info=True;User ID=test;Password=****;connect timeout=9000");
SqlDataAdapter da;
DataSet ds;
int i = 0;
public Form1()
{
InitializeComponent();
timer1.Start();
}
private void Btexit_Click(object sender, EventArgs e)
{
this.Hide();
}
private void Btsave_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-J7D5POF;Initial Catalog=ilswork;Persist Security Info=True;User ID=test;Password=*****;connect timeout=9000");
con.Open();
string query = "UPDATE etman_interior set Name_Arabic='" + txtName_Arabic.Text + "',gender='" + CBgender.Text + "',NATIONALITY='" + CBNATIONALITY.Text + "',username='" + txtusername.Text + "',Time='" + txttime.Text + "',clock='" + txtclock.Text + "' where CIVILIDD='" + txtCIVILIDD.Text + "'";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
sda.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("record updated successfully");
}
private void Btnext_Click(object sender, EventArgs e)
{
con.Close();
if (i < ds.Tables[0].Rows.Count - 1)
{
i++;
txtCIVILIDD.Text = ds.Tables[0].Rows[i]["CIVILIDD"].ToString();
txtName_Arabic.Text = ds.Tables[0].Rows[i]["Name_Arabic"].ToString();
txtName_eng.Text = ds.Tables[0].Rows[i]["Name_eng"].ToString();
CBgender.Text = ds.Tables[0].Rows[i]["gender"].ToString();
CBNATIONALITY.Text = ds.Tables[0].Rows[i]["NATIONALITY"].ToString();
}
txtCIVILIDD.Focus();
}
private void Btlast_Click(object sender, EventArgs e)
{
if (i == ds.Tables[0].Rows.Count - 1 || i != 0)
{
i--;
txtCIVILIDD.Text = ds.Tables[0].Rows[i]["CIVILIDD"].ToString();
txtName_Arabic.Text = ds.Tables[0].Rows[i]["Name_Arabic"].ToString();
txtName_eng.Text = ds.Tables[0].Rows[i]["Name_eng"].ToString();
CBgender.Text = ds.Tables[0].Rows[i]["gender"].ToString();
CBNATIONALITY.Text = ds.Tables[0].Rows[i]["NATIONALITY"].ToString();
}
else
{
MessageBox.Show("You on frist record");
}
}
private void Form1_Load(object sender, EventArgs e)
{
Disp_data();
txtusername.Text = Class1.Txtusername;
mycon.Open();
da = new SqlDataAdapter("select * from [dbo].[etman_interior]", mycon);
SqlCommandBuilder bul = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "[dbo].[etman_interior]");
dataGridView1.DataSource = ds.Tables["[dbo].[etman_interior]"];
this.KeyPreview = true;
this.ActiveControl = txtCIVILIDD;
txtCIVILIDD.Focus();
}
public void Disp_data()
{
mycon.Open();
SqlCommand cmd = mycon.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [dbo].[etman_interior]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
cmd.CommandTimeout = 0;
mycon.Close();
}
private void Timer1_Tick(object sender, EventArgs e)
{
DateTime dateTime = DateTime.Now;
this.time_lbl.Text = dateTime.ToString();
this.txttime.Text = dateTime.ToString("MM/dd/yyyy");
this.txtclock.Text = dateTime.ToString();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true && e.KeyCode == Keys.S)
{
btsave.PerformClick();
}
if (e.Control == true && e.KeyCode == Keys.D)
{
btnext.PerformClick();
}
}
}
}
答案 0 :(得分:0)
这里有很多问题,其中一些正在组合在一起导致您的问题...
using
块中。完成此操作后,您无需关闭它们,因为它们将在退出该块时被隐式Dispose关闭。using
块中,而不是将它们保留为类级字段,可以更好地实施范围,这将有助于解决您的问题。它还可以保证在引发异常时,一切都已关闭并已处理。Btsave_Click
中,您使用SqlDataAdapter的SelectCommand进行更新(而不是Select)。您完全不需要这里的SqlDataAdapter,只需创建一个SqlCommand(请记住,它是IDisposable的,因此将其放在using
块中)。Disp_data
中,您要在对SqlDataAdapter使用相同命令之前调用ExecuteNonQuery
。这意味着它执行了两次查询-删除ExecuteNonQuery行。 ExecuteNonQuery用于SQL之类的事情,例如插入,更新和删除;不选择。其他一些不太重要的提示,以及您暂时认为不值得的事情,但几年后您可能会重新考虑...
Btnext_Click
中,您连续ds.Tables[0].Rows[i]
使用了5次。对其进行重构,以将其存储在您可以重用的变量中,以便仅对这4个方法进行一次调用。与Btlast_Click
类似。考虑重构这两种方法,以便相似的代码块仅编写一次,并可以在两个地方重复使用。