C # Operations classes for WebForm and WinForm connections to SQL Server 2000 databases in. Net

Source: Internet
Author: User
server|web| Data | Database one. This is for C # connection WinForm
Note points: (1) The namespace of this class must be referenced when called
(2) The namespaces referenced in the class
Using System;
Using System.Data;
Using System.Data.SqlClient;
(3) Call Example:
DataSet DS =new DataSet ();
String Sql= "select * from [user]";
Ds=dodatabase.getdataset (SQL);
Datagrid1.datasource=ds. Tables[0]. DefaultView;
The above four behavior is bound to DATAGRID1 using the user table in the 02wangluo database to populate the dataset
This is the call to the GetDataSet method--> This method is mainly faced with the check (select)


String sql= "INSERT into Test (test_xingming) VALUES (" +txtuserid.text+ ")";
Dodatabase.executesql (SQL);
The above two lines are called the Dodatabase database operation class in the ExecuteSQL method to execute the database, delete, change operations, generally written to the Button_Click
(3) Specific operation type Code:
public class Dodatabase
{
Public Dodatabase ()
{
//
TODO: Add constructor logic here
//
}
public static DataSet GetDataSet (String sql)
{
SqlConnection conn=new SqlConnection ();
DataSet ds=new DataSet ();
Try
{

Conn=new SqlConnection ("server=localhost;uid=sa;pwd=;d Atabase=02wangluo");
SqlDataAdapter Sda=new SqlDataAdapter (Sql,conn);
Sda. Fill (DS);
return DS;

}
catch (Exception ex)
{
Throw (ex);
}
Finally
{
Conn. Close ();
}
}
public static int ExecuteSQL (String sql)
{
SqlConnection conn=new SqlConnection ();
Try
{
Conn=new SqlConnection ("server=localhost;uid=sa;pwd=;d Atabase=02wangluo");
SqlCommand sqlCmd =new SqlCommand (sql,conn);
Conn. Open ();
return Sqlcmd.executenonquery ();
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Conn. Close ();
}
}
}

Two asp.net operations class for C # Operations SQL SERVER 2000 database
(1) Namespace in class:
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Collections;
Using System.Configuration;
(2) in Web.config <configuration> the following line joins the database connection string
<appSettings>
<add key= "sqlconn" value= "Data source= (local); Initial Catalog=02wangluo;user Id=sa; Password= "/>
</appSettings>
(2) Operation Class Code:
public class Dodatabase
{
public static SqlConnection sqlconn = new SqlConnection ();
Public Dodatabase ()
{

}
public static DataSet GetDataSet (String sql)
{
DataSet ds = new DataSet ();

Try
{
sqlconn = new SqlConnection (configurationsettings.appsettings["sqlconn"));
SqlDataAdapter sqlapt = new SqlDataAdapter (SQL, sqlconn);
Sqlapt.fill (DS);
return DS;
}
catch (Exception ex)
{
Throw (ex);
}
}

public static int ExecuteSQL (String sql)
{
Try
{
sqlconn = new SqlConnection (configurationsettings.appsettings["sqlconn"));
SqlCommand sqlCmd =new SqlCommand (sql,sqlconn);
Sqlconn.open ();
return Sqlcmd.executenonquery ();
}
catch (Exception ex)
{
Throw (ex);
}
Finally
{
Sqlconn.close ();
}
}
}
(4) Call Example: slightly



Related Article

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.