如何一次打开连接而不是两次 [英] How do I open the connection one time not two time

查看:54
本文介绍了如何一次打开连接而不是两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为这段代码做最好的事情我想打开连接一次并绑定两个下拉列表。代码工作正常,但我希望学习最好一次使用打开和关闭连接

  string  str1 =  选择eq_type_name +' - '+ eq_type_name_arabic为txtField,eq_type_no来自equit_type;; 
string str2 = SELECT ID,name来自MendCompany;
OleDbConnection conn = new OleDbConnection(cc);
OleDbCommand cmd = new OleDbCommand(str1,conn);
conn.Open();

Dr_device.DataSource = cmd.ExecuteReader();
Dr_device.DataTextField = txtField;
Dr_device.DataValueField = eq_type_no;
Dr_device.DataBind();
conn.Close();
conn.Open();
cmd.CommandText = str2;
Dr_kind0.DataSource = cmd.ExecuteReader();
Dr_kind0.DataTextField = name;
Dr_kind0.DataValueField = ID;
Dr_kind0.DataBind();
conn.Close();
Dr_device.Items.Insert( 0 اخترالجهاز);
Dr_kind0.Items.Insert( 0 **اخترشركةالإصلاح**);





我的尝试:



打开连接2次是我的累了

解决方案

这不是太明显的事情之一,我觉得我错过了什么。但在您的代码中,您有



 Dr_device.DataBind(); 
conn.Close();
conn.Open();
cmd.CommandText = str2;





您关闭连接并自行重新打开。当您在代码中先前打开连接时删除conn.Close和conn.Open并在代码中将其关闭得更远。


如前所述,您关闭并重新打开然后连接即可没理由这样做。 Afetr打开连接,获取所需的数据,执行所需的操作,然后关闭连接。单个连接中允许的操作数量没有限制。



使用连接,命令等时,请记住:

- 使用陈述

- 尽可能晚开启

- 尽快关闭(但在两者之间进行所有操作)

- 处理错误



例如,看看正确执行数据库操作 [ ^ ]

i wanna to do the best for this code i wanna to open the connection one time and bound two dropdownlist . the code worked correctly but i wish to learn the best to use open and close connection one time

string str1 = "select eq_type_name + '-'+ eq_type_name_arabic as txtField,eq_type_no from equit_type;";
               string str2 = "SELECT ID, name FROM MendCompany";
               OleDbConnection conn = new OleDbConnection(cc);
               OleDbCommand cmd = new OleDbCommand(str1, conn);
               conn.Open();

               Dr_device.DataSource = cmd.ExecuteReader();
               Dr_device.DataTextField = "txtField";
               Dr_device.DataValueField = "eq_type_no";
               Dr_device.DataBind();
               conn.Close();
               conn.Open();
               cmd.CommandText = str2;
               Dr_kind0.DataSource = cmd.ExecuteReader();
               Dr_kind0.DataTextField = "name";
               Dr_kind0.DataValueField = "ID";
               Dr_kind0.DataBind();
               conn.Close();
               Dr_device.Items.Insert(0, " اختر الجهاز");
               Dr_kind0.Items.Insert(0, "**اختر شركة الإصلاح**");



What I have tried:

open connection 2 time is my tired

解决方案

No this is one of those things that is too glaringly obvious that I feel like I am missing something. But in your code you have

Dr_device.DataBind();
               conn.Close();
               conn.Open();
               cmd.CommandText = str2;



You close the connection and are re-openning it yourself. Remove the conn.Close and conn.Open as you open the connection earlier in your code and close it farther down in your code.


As already stated you close and reopen then connection and you have no reason to do that. Afetr opening the connection, fetch the data you need, do the operations you want to and then close the connection. There is no limit how many operations are allowed within a single connection.

When working with connections, commands, and so on, remember to:
- use using statements
- open as late as possible
- close as soon as possible (but do all operations in between)
- handle errors

For examples, have a look at Properly executing database operations[^]


这篇关于如何一次打开连接而不是两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆