C#的新手 - 数据库问题 [英] New to C# - DB question

查看:59
本文介绍了C#的新手 - 数据库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯使用旧学校的ADO,在那里我可以创建像:


objConn.Open(sConnectString);


objRecordSet 。打开(从用户中选择*,其中UID =''+ sUserID +"''",

objConn);


然后我有一个记录集集合,我可以根据特定的SQL查询过滤或逐步完成

等。


.NET中是否采用了类似的方法?我看到的项目如


DataSource

TableAdapter

DataSet


但是我这样做没有看到针对特定的

表或一组表执行SQL的特定方法。我将我的数据源定义到我的项目

我可以将网格或字段视图拖放到我的表单中,但我没有

想要这种类型的功能。我想要用户输入,然后在我的数据库中执行

查询。


有人能指出我正确的方向吗?


提前致谢!

I am used to using old school ADO where I might create things like:

objConn.Open( sConnectString);

objRecordSet.Open ("Select * from users where UID='' + sUserID + "''",
objConn);

Then I have a recordset collection that I can filter on or step through
etc. based on a specific SQL query.

Is there a similar methodology employed in .NET? I see items like

DataSource
TableAdapter
DataSet

But I do not see a specific method for executing SQL against a specific
table or set of tables. I have my data source defined to my project
and I can drag and drop grid or field views to my form but I do not
want that type of functionality. I want user inputs that I then do a
lookup in my database with.

Can someone point me in the right direction?

Thanks in advance!

推荐答案

你好,


是的,你是在正确的轨道上。

首先,你对System.Data命名空间感兴趣,也称为

ADO.NET

在那里你会发现一些子命名空间用于特定数据库

连接,还有一些通用类对所有数据库都有用。

类型。


..NET实现了一个断开连接的架构,你将基本上将b $ b暂时连接到数据库,执行一些操作(选择,删除,

更新,创建),并将结果放入某个对象,然后断开连接。

有一个名为数据集的类。其中可以包含一组

DataTables

dataTable可以存储select的结果。针对数据库。我们

通常使用DataAdapter。为我们管理SQL的简单表格

操作(即单个表格CRUD)。 DataAdapter(例如

System.Data.SqlClient.SqlDataAdapter)可以使用DataConnection(例如,作为System.Data.SqlClient.SqlConnection的
)对象,以及一些

选择语句(例如Select * From sysobjects)作为字符串,并填写

数据表。


如果你想要执行不返回

整个结果集的简单数据库语句,可以使用DbCommand对象(例如

System.Data.SqlClient.SqlCommand )。

命令对象也接受一个连接对象,一个字符串

是你的sql语句。使用命令对象,你有3个方法,

你应该关注自己只有2作为初学者:

ExecuteNonQuery()和ExecuteScalar()

前者允许你只执行一些SQL,它将返回

受影响的行数。后一个选项允许你执行一些sql

你期望返回一个结果,称为''Scalar''。

例如sql这样的sql ;选择Count(*)FROM sysobjects"你需要一个整数结果,而不是带有行的表格。


无论如何,选择你正在使用的数据库类型并查看

相对命名空间。请注意,您也可以在设计中使用数据适配器和数据集进行相当多的工作,这一切都非常好。

记录在MSDN中。


希望这会有所帮助,

史蒂文

Hi there,

Yes you are along the right track.
Firstly, you are interested in the System.Data namespace, also known as
ADO.NET
In there you will find a few sub namespaces for specific database
connections, and also some generic classes useful for all database
types.

..NET implements a disconnected architecture, where you will essentially
connect to a DB temporarily, perform some action (select, delete,
update, create), and put the results into some object, then disconnect.
There is a class called "Dataset" which can contain a collection of
"DataTable"s
A dataTable can store the result of a "select" against a database. We
generally use a "DataAdapter" to manage the SQL for us for simple table
operations (ie. a single table CRUD). A DataAdapter (such as the
System.Data.SqlClient.SqlDataAdapter) can take a DataConnection (such
as a System.Data.SqlClient.SqlConnection) object, along with some
select statement (eg Select * From sysobjects) as a string, and fill a
data table.

If you want to perform simple database statements that don''t return a
whole result set, you can use the DbCommand objects (such as a
System.Data.SqlClient.SqlCommand).
The command object also takes a connection object, and a string which
is your sql statement. With the command object you have 3 methods, of
which you should concern yourself with just 2 as a beginner:
ExecuteNonQuery() and ExecuteScalar()
The former allows you to just execute some SQL, and it will return the
number of rows affected. The latter option lets you execute some sql
where you expect a single result being returned, known as a ''Scalar''.
Example of this is sql such as "Select Count(*) FROM sysobjects" which
you expect a single integer result, rather than tables with rows.

Anyways, pick which database type you are using and check out the
relative namespace. Note that you can also do quite a lot in design
mode with data adapters and datasets, and this is all very well
documented in MSDN.

Hope this helps,
Steven


哦,如果你急于需要完成一些DB

功能,您可以随时添加对旧adodb com

内容的引用,并使用您熟悉的记录集。但是如果你有时间,肯定会学习.NET中的新架构。


我记得4年前我在uni上做过.NET主题的时候当我第一次介绍它的时候。
。我来自ASP背景,我知道COM

但不知道ADO.NET所以我在第一次任务时完成了这个:完成

使用ADO记录集的要求!幸运的是我被要求

重新提交,否则我现在将以清洁窗户为生......

Oh also, if you are in a rush and need to complete some DB
functionality, you could always add a reference to the old adodb com
stuff and just use the recordsets you are familiar with. But if you
have time, definately learn the new architecture in .NET.

I remember when I did a .NET subject at uni in 4 years ago when I was
first introduced to it. I came from ASP background and I knew about COM
but not ADO.NET so I did exactly that on my first assignment: completed
the requirements using an ADO recordset! Luckily I was asked to
resubmit, otherwise I''d be cleaning windows for a living now...




谢谢你,史蒂夫。我最后为这个小测试做的是:


private void btnSubmit_Click(object sender,EventArgs e)

{

int Privelege;

string SQL;

OleDbDataReader Reader;

string sConnect =" Provider = Microsoft.Jet.OLEDB.4.0; Data

来源= G:\\ Media Manager.mdb" ;;

OleDbConnection objConnection = new OleDbConnection

(sConnect);


if(this.txtPassWd.Text.Length< 1 ||

this.txtUserID.Text.Length< 1)

{

MessageBox.Show(你必须填写两个字段!);

返回;

}


objConnection.Open();


尝试

{

SQL =" Select * from Users其中username =''" +

this.txtUserID.Text +"''AND password =''" + this.txtPassWd.Text +"'';" ;;

//在用户表中查找用户ID

//如果我们找到它,验证密码和设置

privledge。


OleDbCommand oMyCommand = new OleDbCommand(SQL,

objConnection);

Reader = oMyCommand.ExecuteReader();


if(Reader.HasRows)

{

//登录好在这里

MessageBox.Show(登录很好);

Reader.Read();

Privelege = Reader.GetInt32(3) ;

}

其他

{

MessageBox.Show(登录不好);

}

}

终于

{

objConnection.Close();

//Reader.Close();

}

}


文档说要确保我关闭了读者,但编译器抱怨了它的b $ b ...不确定原因。

Thanks a ton, Steve. What I ended up doing for this little test was:

private void btnSubmit_Click(object sender, EventArgs e)
{
int Privelege;
string SQL;
OleDbDataReader Reader;
string sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source = G:\\Media Manager.mdb";
OleDbConnection objConnection = new OleDbConnection
(sConnect);

if (this.txtPassWd.Text.Length < 1 ||
this.txtUserID.Text.Length < 1)
{
MessageBox.Show("You MUST fill both fields in!");
return;
}

objConnection.Open();

try
{
SQL = "Select * from Users where username=''" +
this.txtUserID.Text + "'' AND password=''" + this.txtPassWd.Text + "'';";
// Find USER ID in users table
// if we find it, validate the password and set the
privledge.

OleDbCommand oMyCommand = new OleDbCommand(SQL,
objConnection);
Reader = oMyCommand.ExecuteReader();

if (Reader.HasRows)
{
// Login is good here
MessageBox.Show("Login is good");
Reader.Read();
Privelege = Reader.GetInt32(3);
}
else
{
MessageBox.Show("Login is bad");
}
}
finally
{
objConnection.Close();
//Reader.Close();
}
}

The docs said to ensure I closed the reader but the compiler complained
about it...Not sure why.


这篇关于C#的新手 - 数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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