cmd.ExecuteNonQuery()在我的程序中到底做了什么 [英] What exactly does cmd.ExecuteNonQuery() do in my program

查看:187
本文介绍了cmd.ExecuteNonQuery()在我的程序中到底做了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string connection = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=D:\\it101\\LoginForm\\App_Data\\registration.mdb";

string query = "INSERT INTO [registration] ([UserID] , [Name] , [Contact_no] , [City]) values (123, 'abc' ,12345, 'pqr')";

OleDbConnection con = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;

con.Open();
cmd.ExecuteNonQuery();

如果我在程序末尾不写cmd.ExecuteNonQuery()怎么办? 如果需要执行查询,为什么要写成executeNonquery()而不是executeQuery()?

And what if i do not write cmd.ExecuteNonQuery() at the end of my program? And if the query needs to be executed why is it written executeNonquery() instead of executeQuery()?

推荐答案

如果我们要处理数据库,将会发生两件事:修改,检索

if we want to deal with database two things will happen i.e; Modifying,Retrieving

修改:

在修改部分,我们有插入删除更新,...查询.为此,我们需要使用ExecuteNonQuery命令.为什么因为我们不查询数据库,所以我们正在修改.

In Modifying Section,we have Insert, Delete ,Update,...queries.so for this we need to use ExecuteNonQuery command.why because we are not querying a database, we are modifying.

语法:

cmd.ExecuteNonQuery方法

正在检索:

在此我们使用 Select 语句查询数据库.为此,我们使用ExecuteReader(),ExecuteScalar()

In this we query a database by using Select Statement.For this we use ExecuteReader(),ExecuteScalar()

如果选择查询返回多个记录,则需要使用ExecuteReader()

If select Query return more than one record.we need to use ExecuteReader()

如果选择查询仅返回一条记录,则需要使用ExecuteScalar()

If select Query return only one record.we need to use ExecuteScalar()

语法:

cmd.ExecuteReader()方法

cmd.ExecuteScalar()方法

上述语句(ExecuteReader(),ExecuteScalar(),SqlCommand.ExecuteNonQuery())用于执行在SqlCommand中给出的命令语句.如果不使用,则不会执行命令.

The above statements(ExecuteReader(),ExecuteScalar(),SqlCommand.ExecuteNonQuery()) are used to execute command statement which you give in SqlCommand.If you dont use, your command not be executed.

这篇关于cmd.ExecuteNonQuery()在我的程序中到底做了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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