在操作或服务器完成之前经过的超时时间



当我从另一台电脑运行应用程序时发现的错误(打开链接查看错误图片

我的代码有问题吗?我是编码错误的初学者:

(超时过期。在操作或服务器完成之前经过的超时时间(

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();
}
}
}
}

这里有很多问题,其中一些问题结合在一起导致了您的问题。。。

  1. SqlConnection、SqlCommand和SqlDataAdapter都是可IDisposable的,因此每个都应该在using块中。完成后,就不需要关闭它们,因为隐式Dispose在退出块时会关闭它们
  2. 通过将内容放在using块中,而不是将它们保留为类级字段,您可以更好地执行作用域,这将有助于解决您的问题。它还保证在抛出异常时,事物是Closed和Disposed
  3. 您的代码容易受到SQL注入攻击:避免使用字符串串联来创建查询,而是使用参数。仔细阅读,这很重要
  4. Btsave_Click中,您使用SqlDataAdapter的SelectCommand来执行更新(而不是选择(。这里根本不需要SqlDataAdapter,只需创建一个SqlCommand(记住,它是IDisposable的,所以把它放在using块中(
  5. Disp_data中,先调用ExecuteNonQuery,然后再对SqlDataAdapter使用相同的命令。这意味着它将执行您的查询两次-删除ExecuteNonQuery行。ExecuteNonQuery用于插入、更新和删除SQL;而不是选择

其他一些不太重要的提示,以及目前你认为不值得的事情,但几年后你可能会重新考虑。。。

  1. 与其复制连接字符串,不如将其存储在可重复使用的常量字符串中
  2. 请考虑将连接字符串放在配置文件中,然后使用System.Configuration.ConfigurationManager获取它
  3. 从用户界面代码中分离出获取/存储数据的代码是个好主意。目前,所有这些都被挤在一起,这导致代码很难维护
  4. Btnext_Click中,您可以连续使用ds.Tables[0].Rows[i]5次。重构它,将其存储在您重用的变量中,这样它只执行一次这4个方法调用。类似于Btlast_Click。考虑重构这两个方法,这样类似的代码块只写一次,并从两个地方重用
  5. 打字"frist">

最新更新