您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在ADO.Net面向连接模式下在SQL数据库中插入行

如何在ADO.Net面向连接模式下在SQL数据库中插入行

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

DataReader是连接的体系结构,因为它保持连接打开,直到获取所有记录

如果要插入ADO.NET,则应执行以下步骤:

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", Text@R_457_2419@1.text);
cmd.Parameters.AddWithValue("pass", Text@R_457_2419@2.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();


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


    }
SQLServer 2022/1/1 18:34:19 有516人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶