在哪里编写代码 [英] Where to write coding

查看:175
本文介绍了在哪里编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;_
	Data Source=..\\..\\myDB.mdb;User ID=Admin;Password=";

OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
string query = "SELECT studentID, firstName, lastName, birthDate, _
				address, contactNo FROM studentInfo";
OleDbDataAdapter oleDA = new OleDbDataAdapter(query,conn);


在哪里键入此编码以创建数据集


Where to type this coding for creating a data set

推荐答案

这取决于您将要使用的数据.
通常情况下,您不会打开数据库连接并保持打开状态:您应该打开,执行事务并再次关闭它-数据库连接是一种稀缺资源,在不使用时不应该保持打开状态阻止其他用户进入.

你想做什么?这将影响您需要在何处放置代码.
It depends on what you are going to do with it.
Under normal circumstances, you do not open a database connection and leave it open: You should open, do your transactions, and close it again - database connections are a scarce resource and should not be held open when they are not in use as it can prevent other users from getting in.

What are you trying to do? That will affect where you need to put your code.


您的问题不清楚.您可以在需要访问数据的任何地方使用此代码.请记住关闭打开的连接.

要从适配器获取数据集,可以使用数据适配器的Fill方法,该方法具有四个重载选项.

Your question not clear. You can have this code wherever you need to access data. Remember to close the connection which you opened.

To get dataset from the adapter you can use the data adapter''s Fill method which has four overloaded options.

DataSet ds = new DataSet("Test");
oleDA.Fill(ds);



现在的问题是是否保留这些对象.我认为这是您问题的何处"部分.好吧,这取决于必要性.适配器和连接字符串可以保留全局范围.但是鉴于数据集,如果需要在许多地方使用它,则可以将数据集存储在表单的全局变量中,否则无需保留.但是在需要时打开连接,使用后将其关闭.

通常,将所有必需的数据从数据库中带到内存中,然后使用业务逻辑来访问特定数据.请记住应该保持平衡.



Now the question is Preserve these objects or not. I think that is the ''Where'' part of your question. Well it depends on necessity. Adapter and the connection string you could keep global scope. But in view of dataset, If many places you need to use this then you can store dataset in a form''s global variable otherwise no need to preserve. But open the connection whenever need and close it after used.

As a general rule, bring all the required data from the database to the memory, then use business logic to access specific data. Keep in mind there should be a balance.


umasankari1234,

您可以将其添加到Load_Form事件处理程序中.
希望对您有所帮助,
:)
Hi umasankari1234,

You can add it in the Load_Form event handler.

I hope this help,
:)


这篇关于在哪里编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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