如何在ADO.Net面向连接模式下在SQL数据库中插入行
我有一个数据库,其中一个表的名称为如何在ADO.Net面向连接模式下在SQL数据库中插入行,sql,ado.net,Sql,Ado.net,我有一个数据库,其中一个表的名称为Registration,用于注册用户 它只有两列,一列是用户名,另一列是密码 名为Register.aspx的页面用于注册成员,该页面有两个文本框,一个用于获取用户名(textbox1),一个用于获取密码(textbox2),还有一个按钮用于在数据库中插入这些值 主要问题是我们不能这样写语句: Insert into Registration (Username, password) values ('TextBox1.text','TextBox2.tex
Registration
,用于注册用户
它只有两列,一列是用户名
,另一列是密码
名为Register.aspx
的页面用于注册成员,该页面有两个文本框,一个用于获取用户名(textbox1)
,一个用于获取密码(textbox2)
,还有一个按钮用于在数据库中插入这些值
主要问题是我们不能这样写语句:
Insert into Registration (Username, password)
values ('TextBox1.text','TextBox2.text')
我使用的是ADO.net面向连接的模式,我在谷歌上搜索过,但没有找到任何方法在连接模式下在SQL数据库中插入行。请提供插入此行的方法?ADO.NET具有支持连接模式的DataReader。其他的都断开了
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();
}
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();
}
}
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();
}
}
但是ADO.net对于企业级开发是无用的。您必须从数据访问层获取数据和抽象,并转到实体,而不是表记录
使用ORM,卢克
但是ADO.net对于企业级开发是无用的。您必须从数据访问层获取数据和抽象,并转到实体,而不是表记录
使用ORM,卢克 对,教年轻的帕达瓦人如何泄漏资源,留下一个永不关闭的连接。另外,试着向下面的部落成员展示如何在异常情况下关闭连接。@ArtemG从不关闭连接?@syedmohsin你修复了帖子,在catch和not only中添加了con.close(),并试着问一句,制造惊讶的大眼睛?)我的评论日期——2013年11月15日,最后编辑日期——2015年4月2日。很好的尝试,但不是。对,教年轻的帕达瓦人如何泄漏资源,留下一个永不关闭的连接。另外,试着向下面的部落成员展示如何在异常情况下关闭连接。@ArtemG从不关闭连接?@syedmohsin你修复了帖子,在catch和not only中添加了con.close(),并试着问一句,制造惊讶的大眼睛?)我的评论日期——2013年11月15日,最后编辑日期——2015年4月2日。很好的尝试,但是没有。哈哈:)你是对的@Artem,我很忙,所以我很快就做到了:)顺便说一句,我是伊朗人,不是印度人。代码已更正。谢谢。哈哈:)你说得对@Artem,我当时很忙,所以我很快就把事情办好了:)顺便说一句,我是伊朗人,不是印度人。代码已更正。谢谢