3

我有一个数据库,其中一个表具有Registration用于注册用户的名称。

它只有两列,一是Username,一是password

一个名为的页面Register.aspx用于注册成员,该页面有两个文本框,一个用于获取Username(textbox1),一个用于获取password(textbox2),一个按钮用于将这些值插入数据库。

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

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

我正在使用 ADO.net 面向连接的模式,我用谷歌搜索,但我没有找到任何方法在连接模式下在 SQL 数据库中插入行。请给我一个插入这一行的想法?

4

3 回答 3

6

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", 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();
     }


    }
于 2012-12-30T13:27:44.267 回答
0
 try
        {

            using (var connection = new SqlConnection(yourConnectionString))
            {
                string queryString = "Select id, name, age from Table where name = @name";
                using (var command = new SqlCommand(queryString, Connection))
                {
                    command.Parameters.AddWithValue("@name", name);

                    Connection.Open();
                    SqlDataReader dataReader = command.ExecuteReader();

                    while (dataReader.Read())
                    {
                        item = new Item(long.Parse(dataReader[0].ToString()),
                                        dataReader[1].ToString(),
                                        int.Parse(dataReader[2].ToString()));
                    }
                    dataReader.Close();
                }

            }
        }
        catch (Exception ex)
        {
           // if exception will happen in constructor of SqlConnection or Command, the // resource can leak but Dispose method will never be called, couse the object was not created yet.        

          // Trace and handle here
            throw;
        }
        finally
        {

        }

但是 ADO.net 对企业发展毫无用处。您必须从数据访问层进行抽象,然后转到实体,而不是表记录 使用 ORM,Luke!

于 2013-11-05T21:09:44.890 回答
-1
using System.Data.SqlClient;

string cnstr = "server=.;database=dbname;user=username;password=password;";
SqlConnection con = new SqlConnection(cnstr);
SqlCommand cmd = new SqlCommand { Connection = con };
cmd.CommandText = "Insert into Registration (Username, password) values ('@user','@pass')";
cmd.Parameters.AddWithValue("user", TextBox1.text);
cmd.Parameters.AddWithValue("pass", TextBox2.text);
try
{
    con.Open();
    cmd.ExecuteNonQuery();
}
catch (Exception)
{
    //something;
}
finally
{
    if (con != null)
       con.Close();
}
于 2012-12-30T13:26:42.637 回答