如何插入在ADO.Net面向连接的模式行SQL数据库 [英] how to insert row in SQL database in ADO.Net Connection oriented mode

查看:124
本文介绍了如何插入在ADO.Net面向连接的模式行SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中一个表有名字注册这是用来注册用户的数据库。

I have a database in which a table has name Registration which is used to register the user.

它只有两个一列用户名键,一个是密码

It has only two column one is Username and one is password.

命名一个页面 Register.aspx 用于注册其有成员两个文本框一个是采取用户名(TextBox1中),一个是采取密码(TextBox2中)和一个按钮插入数据库中,这些价值。

A page named Register.aspx is used for registering the member which have two textbox one is for taking Username(textbox1) and one is for taking password(textbox2) and one button for insert these value in database.

主要的问题是,我们不能写这样的语句:

The Main problem is that we cannot write statement like this :

Insert into Registration (Username, password) 
values ('TextBox1.text','TextBox2.text')

我使用ADO.net面向连接的方式,我用Google搜索,但我没有发现任何方式插入连接模式行中的SQL数据库。请给我一个想法插入该行?

I am using ADO.net Connection oriented mode, I googled but I didn't find any way to insert row in SQL database in connected mode. Please provide me a idea for inserting this row?

推荐答案

ADO.NET具有DataReader的支持连接模式。所有其他断开连接。

ADO.NET has DataReader which supports Connected mode. All else are disconnected.

DataReader的连接架构,因为它使conn​​eection打开,直到所有的记录都取

DataReader is connected architecture since it keeps conneection open untill all records are fetched

如果你想在ADO.NET插入,那么你应该执行以下步骤:

If you want to insert in ADO.NET then you should perform the following steps:

private void btnadd_Click(object sender, EventArgs e)
{
  try
  {
   //create  object  of Connection Class..................
   SqlConnection con = new SqlConnection();

   // Set Connection String property of Connection object..................
  con.ConnectionString = "Data Source=KUSH-PC;Initial Catalog=test;Integrated           Security=True";

 // Open Connection..................
  con.Open();

 //Create object of Command Class................
 SqlCommand cmd = new SqlCommand();

//set Connection Property  of  Command object.............
cmd.Connection = con;
//Set Command type of command object
//1.StoredProcedure
//2.TableDirect
//3.Text   (By Default)

cmd.CommandType = CommandType.Text;

//Set Command text Property of command object.........

cmd.CommandText = "Insert into Registration (Username, password) values ('@user','@pass')";

//Assign values as `parameter`. It avoids `SQL Injection`
cmd.Parameters.AddWithValue("user", TextBox1.text);
cmd.Parameters.AddWithValue("pass", TextBox2.text);

 Execute command by calling following method................
  1.ExecuteNonQuery()
       This is used for insert,delete,update command...........
  2.ExecuteScalar()
       This returns a single value .........(used only for select command)
  3.ExecuteReader()
     Return one or more than one record.

  cmd.ExecuteNonQuery();
  con.Close();


  MessageBox.Show("Data Saved");          
  }
     catch (Exception ex)
     {
            MessageBox.Show(ex.Message);
            con.Close();
     }


    }

这篇关于如何插入在ADO.Net面向连接的模式行SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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