博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库操作必备
阅读量:5146 次
发布时间:2019-06-13

本文共 8432 字,大约阅读时间需要 28 分钟。

using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using System.Data;using System.Windows.Forms;using System.Drawing;namespace DAL{    public static  class DbOperation    {        private static string connStr = "Data Source=127.0.0.1;Initial Catalog=Toy;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, ref ComboBox cb)        {            SqlConnection conn = new SqlConnection(connStr);            conn.Open();            SqlDataAdapter myAdap = new SqlDataAdapter(cmdStr, conn);            DataSet myDatset = new DataSet();            myAdap.Fill(myDatset);            cb.Items.Clear();            for (int i = 0; i < myDatset.Tables[0].Rows.Count; i++)                cb.Items.Add(myDatset.Tables[0].Rows[i][0].ToString());            cb.SelectedIndex = 0;            myAdap.Dispose();        }                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;        }        //用于查询并绑定到datagridview        public static void DbQuery(string cmdStr, ref DataGridView dv)        {            SqlConnection conn = new SqlConnection(connStr);            conn.Open();            SqlDataAdapter myAdap = new SqlDataAdapter(cmdStr, conn);            DataSet myDatset = new DataSet();            myAdap.Fill(myDatset);            dv.DataSource = myDatset.Tables[0].DefaultView;            dv.AllowUserToAddRows = false;            dv.AllowUserToDeleteRows = false;            dv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;            dv.ScrollBars = ScrollBars.Both;            dv.RowsDefaultCellStyle.BackColor = Color.FromArgb(224,224,224) ;            dv.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(255, 255, 255);           // dv.AlternatingRowsDefaultCellStyle =         }        //用于查询并绑定到datagridview        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 void DbEditImage(string editStr,string myImage)        {            //SqlConnection conn = new SqlConnection(connStr);            //conn.Open();            //SqlParameter para = new SqlParameter(myImage, SqlDbType.Image, bytes.Length);            //para.Value = bytes;            //SqlCommand ins_cmd = new SqlCommand(editStr + myImage + ")", conn);            //ins_cmd.Parameters.Add(para);            //ins_cmd.ExecuteNonQuery();            //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;            }        }        //过滤非法字符        public static bool FilterIsSpecial(string str)        {            string flag = str;            if (str == "")            {                return false ;            }            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(" ", "");                if (str == flag)                {                    return false;                }                else                {                    return true;                }            }        }    }}

 

转载于:https://www.cnblogs.com/armanda/p/3611102.html

你可能感兴趣的文章
【转】vim中多标签和多窗口的使用
查看>>
[毕业生的商业软件开发之路]C#异常处理
查看>>
chrome 禁止自动更新
查看>>
一些php文件函数
查看>>
std::min error C2059: 语法错误:“::” 的解决方法
查看>>
Opencv保存摄像头视频&&各种编码器下视频文件占用空间对比
查看>>
「图形学」直线扫描——Bresenham算法改进了中点Bresenham算法?
查看>>
jQuery 给div绑定单击事件
查看>>
Exceptionless 生产部署笔记
查看>>
有关快速幂取模
查看>>
转 ObjExporter Unity3d导出场景地图寻路
查看>>
Linux运维必备工具
查看>>
Ubuntu配置ssh及vnc
查看>>
Kinect学习(3)Kinect for Windows SDK资料下载
查看>>
Java入门——第七天
查看>>
HTML5 Audio时代的MIDI音乐文件播放
查看>>
明确工作职责的重要性
查看>>
ajax方法总结
查看>>
C语言进阶——const 和 volatile 分析09
查看>>
字符串的查找删除
查看>>