当前位置:编程学习 > C#/ASP.NET >>

新手求助ADO.NET的使用方法

最近学习C#的ADO.NET一直没弄明白使用方法,变了下面这个小程序希望在TextBox控件中输入信息存储到数据表Info中,但一直没成功,特来请教各位高手指正错误。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Insert(this.TextBox1.Text, this.TextBox2.Text);
        }
        catch
        {
            throw;
        }
    }

    public void Insert(string 员工姓名, string 联系电话)
    {
        string id = Guid.NewGuid().ToString();
        if (员工姓名 == "科技")
        {
            throw new Exception("姓名不能添加");
        }
        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);

        try
        {
            thisConnection.Open();

            SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT Id,Name,Phone FROM Info", thisConnection);

            SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);

            DataSet thisDataSet = new DataSet();

            thisAdapter.Fill(thisDataSet, "Info");

            DataRow thisRow = thisDataSet.Tables["Info"].NewRow();

            thisRow["Id"] = id;
            thisRow["Name"] = 员工姓名;
            thisRow["Phone"] = 联系电话;

            thisAdapter.Update(thisDataSet, "Info");

            thisConnection.Close();
        }
        catch
        {
            throw;


        }
        finally
        {
            thisConnection.Close();
        }
        Response.Redirect("InfoEdit.aspx", true);
  
    }
}




--------------------编程问答--------------------

string cmdText = "insert into table(字段名1,字段名2) values('" + 参数1 + "','" + 参数2 + "')"; 
OleDbConnection con = MyClass.creatCon(); //创建链接字符串
con.Open(); //打开链接
OleDbCommand cmd = new OleDbCommand(cmdText, con); //创建Command对象
int result = Convert.ToInt32(cmd.ExecuteNonQuery()); //操作
con.Close(); //关闭链接
return result; //返回结果
--------------------编程问答--------------------
引用 1 楼 wiki14 的回复:
C# code

string cmdText = "insert into table(字段名1,字段名2) values('" + 参数1 + "','" + 参数2 + "')"; 
OleDbConnection con = MyClass.creatCon(); //创建链接字符串
con.Open(); //打开链接
OleDbCommand cmd = new OleDbC……


额,这个似乎没体现ADO.NET的用法啊??高手可否告诉下用ADO.NET怎么实现呢?SQL数据库的 --------------------编程问答--------------------
引用 2 楼 xming0925 的回复:
引用 1 楼 wiki14 的回复:
C# code

string cmdText = "insert into table(字段名1,字段名2) values('" + 参数1 + "','" + 参数2 + "')";
OleDbConnection con = MyClass.creatCon(); //创建链接字符串
con.Open(); //打开链接
OleDbComma……


那你说说你理解的ado.net是什么?? --------------------编程问答-------------------- SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(thisAdapter );
thisAdapter .Update(ds); 
ds.Tables[0].AcceptChanges();
--------------------编程问答-------------------- 你倒是告诉我ADO.NET是什么玩意~ --------------------编程问答-------------------- 去看MSDN

听听MSDN webcast --------------------编程问答--------------------
引用 5 楼 wiki14 的回复:
你倒是告诉我ADO.NET是什么玩意~


也不是说ADO.NET,就是想怎样利用DataSet/DataRow 这类去实现这个功能 --------------------编程问答-------------------- 这是我一段程序上的,你改成自己的连接对象应该可以了,不过这是连接式访问,断开式访问的我最近没写
string cnnStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=forum;Integrated Security=True";
SqlConnection cnn = new SqlConnection(cnnStr);
cnn.Open();
string updatecmd = string.Format("insert into zhutianfu values('{0}','{1}','{2}')", this.TextBox1.Text, this.TextBox2.Text, DateTime.Now);
SqlCommand cmd = new SqlCommand(updatecmd, cnn);
cmd.CommandText = updatecmd;
cmd.ExecuteNonQuery(); --------------------编程问答-------------------- 建议楼主百度一下 PDF.NET数据开发框架,对你学习有用。 --------------------编程问答-------------------- 调用SqlCommandBuilder时  查询要有唯一列 或主键。。貌似你有。。

DataRow thisRow = thisDataSet.Tables["Info"].NewRow();

            thisRow["Id"] = id;
            thisRow["Name"] = 员工姓名;
            thisRow["Phone"] = 联系电话;


是不是应该这样啊 :

DataTable dt=thisDataSet.Tables["Info"];
DataRow thisRow=dt.NewRow();
thisRow["Id"] = id;
thisRow["Name"] = 员工姓名;
thisRow["Phone"] = 联系电话;

dt.Rows.Add(thisRow);

不知道是不是这样。。刚看这个。
--------------------编程问答-------------------- ADO.NET一共有两种方式访问数据库,lz用的是通过DataAdapter问数据库.
而一楼用的DataReader访问数据库
这是两种不同的方式,只是执行的效率不同.
上边已经给出了源码,在这里就不做详细解释了. --------------------编程问答-------------------- 多谢各位高手指点,我得再努力学习啊 --------------------编程问答-------------------- thisRow["Id"] = id;
thisRow["Name"] = 员工姓名;
thisRow["Phone"] = 联系电话;

thisDataSet.Tables["info"].Rows.add(thisRow);

thisAdapter.Update(thisDataSet, "Info");

好像你没有把新建的数据行加入数据表,其它应该没错
还有,如果用数据适配器的话就是断开式访问数据库,应该没有必要再执行thisConnection.Open()了
--------------------编程问答-------------------- 没将新行加入"info"表中,可能这个原因
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,