中的存储过程和类

中的存储过程和类

本文介绍了C#中的存储过程和类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





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

public class DataAccess
{
    SqlConnection _MyConnection = new SqlConnection();
    SqlCommand _MyCommand = new SqlCommand();
    SqlDataAdapter _MyAdapter = new SqlDataAdapter();

    private bool OpenConnection()
    {
        try
        {
            _MyConnection.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|ASPNETDB.MDF;Integrated Security=True;User Instance=True";
            if (_MyConnection.State == System.Data.ConnectionState.Closed)
            {
                _MyConnection.Open();
                return true;
            }
            return false;
        }
        catch 
        { 
            return false;
        }
   
    }
    private void CloseConnection()
    {
        _MyConnection.Close();
    }

    public int ExeIUDQuery(string Query)
    {
        OpenConnection();
        _MyCommand.Connection = _MyConnection;
        _MyCommand.CommandText = Query;
        int RowAffectedCount = _MyCommand.ExecuteNonQuery();
        CloseConnection();
        return RowAffectedCount;
    }

    public DataSet ExeSQuery(string Query)
    {
        OpenConnection();
        _MyCommand.Connection = _MyConnection;
        _MyCommand.CommandText = Query;
        _MyAdapter.SelectCommand = _MyCommand;
        DataSet __DS = new DataSet();
        _MyAdapter.Fill(__DS);
        CloseConnection();
        return __DS;
    }
    public DataAccess()
	{
		
	}
}







protected void Button1_Click(object sender, EventArgs e)
    {

        string user = Txtuser.Text;
        string pas = Txtpass.Text;
        string query = "";
        query = "INSERT INTO a (name,family) VALUES ( + user + ,+ pas+ );
        DataAccess DA = new DataAccess();
        DA.ExeIUDQuery(query);
    }



推荐答案


这篇关于C#中的存储过程和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:09