码迷,mamicode.com
首页 > 数据库 > 详细

BS架构数据库操作必备

时间:2014-06-07 21:59:54      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:datagridview   c   style   class   blog   code   

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Collections;
//using System.Windows.Forms;
namespace Test.Utility
{
    public static class DbOperation
    {
        private static string connStr = "Data Source=58.57.32.11,1347;Initial Catalog=CRM;Persist Security Info=True;User ID=sa;Password=521777yesu!#%@$^";



        public static string DbQueryCount(string cmdStr)
        {



            string result = "";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlDataAdapter myAdap = new SqlDataAdapter(cmdStr, conn);
            DataSet myDatset = new DataSet();
            myAdap.Fill(myDatset);

            myAdap.Dispose();

            int a = 0;
            int b = 0;
            a = myDatset.Tables[0].Rows.Count;
            b = myDatset.Tables[0].Columns.Count;

            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < b; j++)
                {
                    result = result + myDatset.Tables[0].Rows[i][j].ToString();
                    result = result + "   ";
                }
                result = result + "\n";
            }



            return result;

        }

        //public static void checkListBind(string cmdStr, ref CheckedListBox clb)
        //{


        //    SqlConnection conn = new SqlConnection(connStr);
        //    conn.Open();
        //    SqlDataAdapter myAdap = new SqlDataAdapter(cmdStr, conn);
        //    DataSet myDatset = new DataSet();
        //    myAdap.Fill(myDatset);

        //    for (int i = 0; i < myDatset.Tables[0].Rows.Count; i++)
        //        clb.Items.Add(myDatset.Tables[0].Rows[i][0].ToString());

        //    //clb.SelectedIndex = 0;

        //    myAdap.Dispose();
        //}

        public static void ComboxBind(string cmdStr,string ViewName,string UserName, ref DropDownList dl)
        {

            int myCount = 0;
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlDataAdapter myAdap = new SqlDataAdapter(cmdStr, conn);
            myAdap.SelectCommand.CommandType = CommandType.StoredProcedure;


            SqlParameter spt1 = new SqlParameter("@MyCount", SqlDbType.VarChar, 20);

            spt1.Direction = ParameterDirection.Output;

            spt1.Value = myCount;

            myAdap.SelectCommand.Parameters.Add(spt1);



            SqlParameter spt2 = new SqlParameter("@ViewName", SqlDbType.VarChar, 20);

            spt2.Direction = ParameterDirection.Input;

            spt2.Value = ViewName;

            myAdap.SelectCommand.Parameters.Add(spt2);


            SqlParameter spt3 = new SqlParameter("@UserName", SqlDbType.VarChar, 20);

            spt3.Direction = ParameterDirection.Input;

            if (UserName == "")
            {
                dl.Visible = false;
                return;
            }
          
            spt3.Value = UserName;
     
            myAdap.SelectCommand.Parameters.Add(spt3);


            DataSet myDatset = new DataSet();
            myAdap.Fill(myDatset);

            dl.Items.Clear();
            for (int i = 0; i < myDatset.Tables[0].Rows.Count; i++)
                dl.Items.Add(myDatset.Tables[0].Rows[i][0].ToString());

            dl.SelectedIndex = 0;

            myAdap.Dispose();
        }

        //用于查询并绑定到datagridview
        public static void DbQuery(string cmdStr, string myParameter, ref GridView dv)
        {



            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlDataAdapter myAdap = new SqlDataAdapter(cmdStr, conn);
            myAdap.SelectCommand.CommandType = CommandType.StoredProcedure;


            if (myParameter != "")
            {

                SqlDataAdapter myAdap1 = new SqlDataAdapter("GetPara", conn);
                int myCount = 0;
                myAdap1.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter spt1 = new SqlParameter("@Para_Count", SqlDbType.VarChar, 20);

                spt1.Direction = ParameterDirection.Output;

                spt1.Value = myCount;

                myAdap1.SelectCommand.Parameters.Add(spt1);

                SqlParameter spt2 = new SqlParameter("@Sp_Name", SqlDbType.VarChar, 20);

                spt2.Direction = ParameterDirection.Input;

                spt2.Value = cmdStr;

                myAdap1.SelectCommand.Parameters.Add(spt2);

                DataSet myDataSet1 = new DataSet();
                myAdap1.Fill(myDataSet1);

                string[] myArray = myParameter.Split(@);




                SqlParameter[] myList = new SqlParameter[myDataSet1.Tables[0].Rows.Count];
                for (int i = 0; i < myDataSet1.Tables[0].Rows.Count; i++)
                {

                    //myList[i].ParameterName = myDataSet1.Tables[0].Rows[i][0].ToString();
                    //MessageBox.Show("" + myDataSet1.Tables[0].Rows[i][0].ToString());
                    myList[i] = new SqlParameter();
                    myList[i].ParameterName = myDataSet1.Tables[0].Rows[i][0].ToString();
                    myList[i].SqlDbType = SqlDbType.NVarChar;
                    myList[i].Size = 256;
                    myList[i].Direction = ParameterDirection.Input;
                    myList[i].Value = myArray[i];

                    myAdap.SelectCommand.Parameters.Add(myList[i]);
                }
            }
            DataSet myDatset = new DataSet();
            myAdap.Fill(myDatset);

            if (myDatset.Tables.Count == 0)
            {
                return;
            }
            dv.DataSource = myDatset.Tables[0].DefaultView;






            dv.DataBind();
            //dv.Columns[1].HeaderStyle.Width = new Unit("124"); 
            myAdap.Dispose();



        }
        //用于查询并绑定到datagridview

        public static string DbReturn(string cmdStr)
        {
            string returnStr = "";


            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand myCmd = new SqlCommand(cmdStr, conn);
            if (myCmd.ExecuteScalar() != null)
                returnStr = myCmd.ExecuteScalar().ToString();
            else
                returnStr = "";


            return returnStr;
        }

    

        public static DataTable DbQueryTable(string cmdStr)
        {



            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlDataAdapter myAdap = new SqlDataAdapter(cmdStr, conn);
            DataSet myDatset = new DataSet();
            myAdap.Fill(myDatset);


            myAdap.Dispose();
            return myDatset.Tables[0];


        }





        public static long DbCount(string cmdStr)
        {
            int myCount = 0;


            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand mycmd = new SqlCommand(cmdStr, conn);
            myCount = (int)mycmd.ExecuteScalar();
            conn.Close();



            return myCount;
        }

        public static void DbEdit(string editStr)
        {

            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand mycmd = new SqlCommand(editStr, conn);
            mycmd.ExecuteScalar();
            conn.Close();
        }
        //过滤非法字符
        public static string FilterSpecial(string str)
        {
            if (str == "")
            {
                return str;
            }
            else
            {
                str = str.Replace("", "");
                str = str.Replace("<", "");
                str = str.Replace(">", "");
                str = str.Replace("%", "");
                str = str.Replace("‘delete", "");
                str = str.Replace("‘drop", "");
                str = str.Replace("‘alter", "");
                str = str.Replace("‘add", "");
                str = str.Replace("‘‘", "");
                str = str.Replace("\"\"", "");
                str = str.Replace(",", "");
                str = str.Replace(".", "");
                str = str.Replace(">=", "");
                str = str.Replace("=<", "");
                str = str.Replace("-", "");
                str = str.Replace("_", "");
                str = str.Replace(";", "");
                str = str.Replace("||", "");
                str = str.Replace("[", "");
                str = str.Replace("]", "");
                str = str.Replace("&", "");
                str = str.Replace("#", "");
                str = str.Replace("/", "");
                str = str.Replace("-", "");
                str = str.Replace("|", "");
                str = str.Replace("?", "");
                str = str.Replace(">?", "");
                str = str.Replace("?<", "");
                //str = str.Replace(" ", "");
                return str;
            }
        }
    }
}
bubuko.com,布布扣

 

BS架构数据库操作必备,布布扣,bubuko.com

BS架构数据库操作必备

标签:datagridview   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/armanda/p/3774862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!