C # ADO. NET Help class,

Source: Internet
Author: User

C # ADO. NET Help class,

Using System; using System. collections. generic; using System. linq; using System. text; using System. data; using System. data. sqlClient; namespace DBComm {static class DBCommand {public class DBParameters {private SqlCommand m_owner = null; public DBParameters (SqlCommand owner) {m_owner = owner;} public SqlParameterCollection P () {return m_owner.Parameters ;}}; public static bool BulkToDB (string tabna Me, DataTable dt, params string [] destColumnNames) {bool bRet = false; do {if (dt = null) break; if (dt. rows. count = 0) break; using (SqlConnection conn = DBConn. getConn () {if (conn = null) break; SqlBulkCopy bulkcopy = new SqlBulkCopy (conn); if (bulkcopy = null) break; bulkcopy. destinationTableName = tabname; bulkcopy. bulkCopyTimeout = 30; if (destColumnNames. length = 0) {foreach (DataC Olumn col in dt. columns) bulkcopy. columnMappings. add (col. columnName, col. columnName);} else {if (destColumnNames. length = dt. columns. count) {for (int I = 0; I <destColumnNames. length; ++ I) {bulkcopy. columnMappings. add (dt. columns [I]. columnName, destColumnNames [I]) ;}} bulkcopy. batchSize = dt. rows. count; try {bulkcopy. writeToServer (dt);} catch (System. exception e) {string err = e. message; Break;} finally {bulkcopy. close () ;}} bRet = true;} while (false); return bRet;} public static DBParameters ExecProcNonQuery (string proc_name, object [] paraValues) {using (SqlConnection conn = DBConn. getConn () {SqlCommand cmd = new SqlCommand (); cmd. connection = conn; cmd. commandType = CommandType. storedProcedure; cmd. commandText = proc_name; AddInParaValues (cmd, paraValues); cmd. execute NonQuery (); return new DBParameters (cmd) ;}} public delegate T [] FillValues <T> (SqlDataReader reader); public static T [] QuerySomes <T> (string SQL, fillValues <T> fill) {using (SqlConnection conn = DBConn. getConn () {T [] result = null; SqlCommand cmd = new SqlCommand (); cmd. connection = conn; cmd. commandText = SQL; SqlDataReader reader = null; lock (reader = cmd. executeReader () {try {result = f Ill (reader);} catch (Exception e) {throw new Exception (e. stackTrace);} finally {reader. close () ;}} return result ;}} public delegate object FillValue (SqlDataReader reader); public static object QuerySome (string SQL, FillValue fill) {using (SqlConnection conn = DBConn. getConn () {object result = null; SqlCommand cmd = new SqlCommand (); cmd. connection = conn; cmd. commandText = SQL; SqlDataRe Ader reader = null; lock (reader = cmd. executeReader () {try {result = fill (reader);} catch (Exception e) {throw new Exception (e. stackTrace);} finally {reader. close () ;}} return result ;}} public static object FillResultValue (SqlDataReader reader) {object o = null; if (reader. read () {o = reader. getValue (0) ;}return o ;}public static bool QueryBoolean (string SQL) {return Convert. toBoo Lean (QuerySome (SQL, new FillValue (FillResultValue);} public static byte [] QueryBytes (string SQL) {return (byte []) (QuerySome (SQL, new FillValue (FillResultValue);} public static int QueryInteger (string SQL) {return Convert. toInt32 (QuerySome (SQL, new FillValue (FillResultValue);} public static string QueryStr (string SQL) {return QuerySome (SQL, new FillValue (FillResultValue) as string;} p Rivate static string [] FillStrsValue (SqlDataReader reader) {List <string> lststr = new List <string> (); while (reader. read () {lststr. add (reader. getString (0);} return lststr. toArray ();} public static string [] QueryStrs (string SQL) {return QuerySomes (SQL, new FillValues <string> (FillStrsValue ));} private static bool [] FillBooleansValue (SqlDataReader reader) {List <bool> lstbool = new List <bool> (); While (reader. read () {lstbool. add (reader. getBoolean (0);} return lstbool. toArray ();} public static bool [] QueryBooleans (string SQL) {return QuerySomes (SQL, new FillValues <bool> (FillBooleansValue);} public static void ExecCmd (string SQL) {using (SqlConnection conn = DBConn. getConn () {SqlCommand cmd = new SqlCommand (); cmd. connection = conn; cmd. commandText = SQL; cmd. executeNonQuery () ;}} /// <Summary> /// obtain the parameter list of the stored procedure /// </summary> /// <param name = "proc_Name"> name of the stored procedure </param> /// <returns> DataTable </returns> private static DataTable GetParameters (SqlConnection conn, string proc_Name) {SqlCommand comm = new SqlCommand ("dbo. sp_sproc_columns ", conn); comm. commandType = CommandType. storedProcedure; comm. parameters. addWithValue ("@ procedure_name", (object) proc_Name); SqlDataAdapter Sda = new SqlDataAdapter (comm); DataTable dt = new DataTable (); sda. fill (dt); return dt ;} /// <summary> /// add parameters and assign values to SqlCommand /// </summary> /// <param name = "comm"> SqlCommand </param> // /<param name = "paraValues"> parameter array (the stored procedure parameter list must be followed) </param> private static void AddInParaValues (SqlCommand comm, params object [] paraValues) {using (SqlConnection conn = DBConn. getConn () {comm. parameters. add (ne W SqlParameter ("@ RETURN_VALUE", SqlDbType. Int); comm. Parameters ["@ RETURN_VALUE"]. Direction = ParameterDirection. ReturnValue; if (paraValues! = Null) {DataTable dt = GetParameters (conn, comm. commandText); int I = 0; foreach (DataRow row in dt. rows) {string key = row [3]. toString (); if (key! = "@ RETURN_VALUE") {int value = int. parse (row [4]. toString (); if (value = 1) {comm. parameters. addWithValue (key, paraValues [I]);} else if (value = 2) // if the value is 2, the output parameter {comm. parameters. addWithValue (key, paraValues [I]). direction = ParameterDirection. output; // comm. parameters [key]. direction = ParameterDirection. output;} comm. parameters [key]. size = Convert. toInt32 (row [7]. toString (); I ++ ;}}}}}}}
Using System; using System. collections. generic; using System. linq; using System. text; using System. data. sqlClient; namespace DBComm {class DBConn {private static string m_connstr; public static string ConnString {get {return m_connstr;} private set {m_connstr = value;} static DBConn () {SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder (); connStr. dataSource = ". "; connStr. initialCatalog = "test"; connStr. integratedSecurity = true; connStr. pooling = true; // enable the connection pool connStr. minPoolSize = 0; // set the minimum number of connections to 0 connStr. maxPoolSize = 100; // set the maximum number of connections to 100 connStr. connectTimeout = 10; // set the timeout value to 10 seconds ConnString = connStr. connectionString; // ConnectDB (ConnString);} public static SqlConnection GetConn () {SqlConnection conn = new SqlConnection (ConnString); conn. open (); return conn ;}}}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.SqlClient;using System.Data;namespace DBComm{    static class DBTableSource    {        public static DataTable GetSource(SqlConnection conn, string strsql)        {            DataTable dt = null;            SqlCommand cmd = null;            SqlDataAdapter ad = null;            try            {                lock (dt = new DataTable())                {                    if (conn is SqlConnection)                    {                        cmd = new SqlCommand(strsql, conn);                        ad = new SqlDataAdapter((SqlCommand)cmd);                    }                    dt.Clear();                    ad.Fill(dt);                }            }            catch (Exception e)            {                throw e;            }            return dt;        }        public static DataTable Source(string strsql)        {            using (SqlConnection conn = DBConn.GetConn())            {                return GetSource(conn, strsql);            }        }    }}




In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

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.