sql表值參數


using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SQLServerDAL //可以修改成實際項目的命名空間名稱
{
 
    public abstract class DbHelperSQL
    {
      
        protected static string connectionString = @"Data Source=.;Initial Catalog=SGPZ;User Id=sa;Password=abc.123";
        public DbHelperSQL()
        {
        }
   

        public static DataSet RunProc(DataTable dt)
        {


            SqlConnection userConnection = new SqlConnection(connectionString);
            SqlCommand userCommand = new SqlCommand("p_student", userConnection);
            userCommand.CommandType = CommandType.StoredProcedure;//采用存儲過程
            userCommand.Parameters.Add("@tstudent", SqlDbType.Structured);//存儲過程參數
            userCommand.Parameters["@tstudent"].Value = dt;//給參數賦值
            userCommand.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter(userCommand);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            return ds;
        }

 

        /**/
        /// <summary>
        /// 執行查詢語句,返回DataSet
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        public static DataSet Query(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }


    }
}

 

 

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data.SqlClient;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          

        }

        private void button1_Click(object sender, EventArgs e)
        {
          
            System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start(); //  開始監視代碼運行時間

            DataSet dt = SQLServerDAL.DbHelperSQL.Query("select  * from student ");
            dataGridView1.DataSource = SQLServerDAL.DbHelperSQL.RunProc( dt.Tables[0]).Tables[0].DefaultView;

            stopwatch.Stop(); //  停止監視
            TimeSpan timespan = stopwatch.Elapsed; //  獲取當前實例測量得出的總時間
            double hours = timespan.TotalHours; // 總小時
            double minutes = timespan.TotalMinutes;  // 總分鍾
            double seconds = timespan.TotalSeconds;  //  總秒數
            double milliseconds = timespan.TotalMilliseconds;  //  總毫秒數
            MessageBox.Show(seconds.ToString());
        }


     
    }

 

 


}

 

 

USE [SGPZ]
GO
/****** Object:  StoredProcedure [dbo].[p_student]    Script Date: 07/05/2013 17:19:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[p_student]
@tstudent  tstudent READONLY
AS
begin
SELECT * INTO #tstudent  FROM @tstudent
UPDATE #tstudent SET STUNAME='周傑倫'
SELECT * FROM #tstudent
DROP TABLE #tstudent
END


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM