A handy data-updating device-sqldataupdater

Source: Internet
Author: User
Data

<summary>
File name: SqlDataUpdater.cs
ABSRTACT: SQL Data Update, it is convenient to read, write, delete operation of single data table, the constructor is outgoing result set by reference.
Sqldataupdater du = new Sqldataupdater (Sqlhere,ref yourdataset)
Call Update (DS) updating data, automatic transaction processing, error messages exist errormessage.
///
Completion Date: 2005-4-21
Author: Weisunding
Current Version: 1.0
</summary>
public class Sqldataupdater
{
Private SqlConnection cn;
Private SqlCommand cmd;
Private SqlDataAdapter da;
Private SqlTransaction Tran;
Private SqlCommandBuilder Cmdbuilder;
private string Err;

public string errormessage
{
Get
{
return err;
}
}

<summary>
Initializing the Data Update device
</summary>
<param name= "SQL" > sql</param> for returning schema
<param name= "DataSet" > Data set by reference </param>
Public Sqldataupdater (String sql,ref DataSet)
{
Err = "";
if (DataSet = null) DataSet = new DataSet ();
cn = New SqlConnection (ZMarketData.DataSetting.ConnectionString);
cmd = new SqlCommand (SQL,CN);
da = new SqlDataAdapter (cmd);
Cmdbuilder = new SqlCommandBuilder (DA);

Da. Fill (DataSet);
}

  public bool Update (DataSet ds)
  {
   bool success = false;
&NBSP;&NBSP;&NBSP;CN. Open ();
   tran = CN. BeginTransaction ();
   cmd. Transaction = Tran;
   try
   { 
    if (ds. GetChanges ()!= null)
    {
     da. Update (ds. GetChanges ());
    }
    success = true;
    tran.commit ();
   }
   catch (Exception ex)
   {
    tran. Rollback ();
    err = ex. message;
    success = false;
   }
   return success;   
  }

~sqldataupdater ()
{
Da. Dispose ();
Cmd. Dispose ();
cn. Dispose ();
}
}

application Example

DataSet ds = new DataSet ();
Sqldataupdater du = new Sqldataupdater ("SELECT * from Public_feedback where 1=2", ref DS);

DataRow dr = ds. Tables[0]. NewRow ();

dr["CreateDate"] = DateTime.Now;
dr["FirstName"] = request["Txtname"];
dr["CompanyName"] = request["Txtcompany"];
dr["Email"] = request["Txtemail"];

Ds. Tables[0]. Rows.Add (DR);
if (Du. Update (DS))
{
Success
}
Else
{
Response.Write (Du. ErrorMessage);
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.