谁能解释?以下代码是如何工作的,逐步解释.... [英] can anyone explain? following code how it works explain step by step....

查看:54
本文介绍了谁能解释?以下代码是如何工作的,逐步解释....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以逐步解释以下代码:


编码:


can anyone explain following code how it works step by step:


Coding:


if (con.State == ConnectionState.Closed)
       {
           con.Open();
       }
       SqlCommand sql = new SqlCommand("select * from category where category_name=''" + txtcategory_name.Text + "''", con);
       if (Convert.ToInt32(sql.ExecuteScalar()) == 0)
       {
           SqlCommand cmd = new SqlCommand("insert into category values(''" + txtcategory_name.Text + "'',''" + txtdescription.Text + "'')", con);
           cmd.ExecuteNonQuery();
           lblmsg.Text = "Submitted";
           con.Close();
       }
       else
       {
           lblmsg.Text = "category Name Already Exists";
       }

推荐答案

SqlCommand sql将检索类别名称等于所选名称的所有值.

ExecuteScalar()仅返回表中的第一行.

如果没有返回任何行,则类别名称不存在.
只有这样,您才能插入txtcategory_nametxtdescription中的文本.

否则,它将返回"category Name Already Exists"消息.
SqlCommand sql will retrieve all values where category name is equal to the name selected.

ExecuteScalar() returns the first row only from the table.

If no row is returned, then the category name does not exist.
Only then you will be able to insert the text from txtcategory_name and txtdescription.

Else it will return "category Name Already Exists" message.


您已经使用过ExecuteScalar,它将返回查询返回的结果集中第一行的第一列.多余的列或行将被忽略.

逐步执行
1.检查连接是否关闭,如果否,则打开它
2.从类别表中获取所有行,其中用户输入了类别
3.如果不还原记录,则代码将插入具有类别名称和描述的类别表.
4.它将插入记录并关闭连接
5.如果记录在数据库中并按用户输入类别,则它将显示消息"类别名称已存在"
You have used ExecuteScalar, which will returns the first column of the first row in the result set returned by the query. Extra columns or rows are ignored.

step by step execution
1. check if the connection is closed, if NO then open it
2. Fetch all rows from category table where category is entered by user
3. if it does not returs records, code will insert in to category table with category name and description.
4. it will insert the record and close the connection
5. if records are in the database with category entered by user then it will put message "Category Name Already Exists"


1)检查连接是否打开或关闭.如果关闭,则打开连接
1) Check Connection is open or close. if closed then open connection
if (con.State == ConnectionState.Closed)
{
con.Open();
}



2)检查类别名称是否为txtcategory_name.Category表中是否存在文本名称



2) Check that category_name with txtcategory_name.Text name is exists in Category table

SqlCommand sql = new SqlCommand("select * from category where category_name='" + txtcategory_name.Text + "'", con);
        if (Convert.ToInt32(sql.ExecuteScalar()) == 0)
        {
          ....Code
        }
        else
        {
            lblmsg.Text = "category Name Already Exists";
        }


3)如果没有,则插入类别并通过con.close()关闭连接


3) if not then Insert Category and close Connection by con.close()

SqlCommand cmd = new SqlCommand("insert into category values('" + txtcategory_name.Text + "','" + txtdescription.Text + "')", con);
            cmd.ExecuteNonQuery();
            lblmsg.Text = "Submitted";
            con.Close();


这篇关于谁能解释?以下代码是如何工作的,逐步解释....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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