.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Tag Archives: soap

Kết nối với SQL server bằng web serice


Phần 1: Tạo ra một web service sử dụng SQL server
Mình sẽ tạo ra một web service đơn giản dùng .NET.
+ Tạo một file DataProvider để tương tác với SQL server. Nội dung đơn giản như sau:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ExampleWebservice
{
    class DataProvider
    {
        protected string connectionString = @"Data Source=PRMHUITEDU-PCSQLEXPRESS;Initial Catalog=TestWebservice;Integrated Security=True";
        public SqlConnection connection;

        public DataProvider()
        {
            this.connection = new SqlConnection(connectionString);
        }

        public bool Connect()
        {
            try
            {
                connection.Open();
            }
            catch (System.Exception e)
            {
                //System.Windows.Forms.MessageBox.Show(_error);
                return false;
            }
            return true;
        }

        public void Disconnect()
        {
            connection.Close();
        }

        #region "Execute_Query"
        //Insert query
        protected bool _ExecuteQuery(string pStrQuery)
        {
            if (connection.State == System.Data.ConnectionState.Closed)
            {
                if (!Connect())
                {
                    return false;
                }
            }

            SqlCommand command = new SqlCommand(pStrQuery, this.connection);
            int kq = 0;
            try
            {
                kq = command.ExecuteNonQuery();
            }
            catch (SqlException el)
            {
            }
            if (kq <= 0)
            {
                return false;
            }
            return true;
        }

        public DataSet _Select(string pStrSelect)
        {
            if (connection.State == System.Data.ConnectionState.Closed)
            {
                if (!Connect())
                {
                    Connect();
                }
            }
            SqlCommand cmd = new SqlCommand(pStrSelect, connection);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            DataSet table = new DataSet();

            adap.Fill(table);
            return table;
        }
        public DataSet _Select(string pStrSelect, string paccount)
        {
            if (connection.State == System.Data.ConnectionState.Closed)
            {
                if (!Connect())
                {
                    Connect();
                }
            }
            SqlCommand cmd = new SqlCommand(pStrSelect, connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@account", SqlDbType.NVarChar).Value = paccount;
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            DataSet table = new DataSet();

            adap.Fill(table);
            return table;
        }
        #endregion
    }
}

Read more of this post

Advertisements
%d bloggers like this: