C# SqlServer操作笔记

window身份验证(作为数据库管理员时候经常使用)
Data Source=服务器名;Initial Catalog=数据库;Integrated Security=true(集成验证);
Sql Server身份验证
Server=服务器名;Database=数据库;uid=用户名;password=密码(无密码的时候可以不用填);
配置文件连接字符串
App.config(注意在配置文件里没有转义字符)
在标签里面添加以下字段
这里写图片描述
1.添加configuration引用
2.获取字符串:string conStr1 = ConfigurationManager.ConnectionStrings[“conStr”].ConnectionString;

 //执行查询sql语句(使用using,会自动释放资源)
 public void SelectData(string sql)
        {
            //每次new完就会自动释放资源
            using (SqlConnection conn=new SqlConnection(conStr1))
            {
                using(SqlCommand cmd=new SqlCommand(sql, conn))
                {
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {                    
                        while (reader.Read())
                        {
                            Console.WriteLine(reader[0].ToString());
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 通用插入语句
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String generateInsertSql(Object obj)
        {
            StringBuilder sql = new StringBuilder("insert into ");

            Type type = obj.GetType();
            //类名 -> 表名
            sql.Append(type.Name);
            sql.Append("(");
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                sql.Append(property.Name);
                sql.Append(",");
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append(") ");
            sql.Append("values(");

            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType.Name.Equals("String"))
                {
                    sql.Append("'");
                }
                sql.Append(property.GetValue(obj, null));
                if (property.PropertyType.Name.Equals("String"))
                {
                    sql.Append("'");
                }
                sql.Append(",");
            }
            sql.Remove(sql.Length - 1, 1);
            sql.Append(") ");

            return sql.ToString();
        }

        /// <summary>
        /// 通用删除
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string generateDeleteSql(Object obj)
        {
            // StringBuilder sql = new StringBuilder();
            StringBuilder sql = new StringBuilder("delete from ");

            Type type = obj.GetType();
            //类名 -> 表名
            sql.Append(type.Name);          
            return sql.ToString();
        }
        //执行查询sql语句
        public void Command(string str,SqlConnection connection)
        {          
            connection.Open();
            //创建SqlCommand操作对象
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            //设置如何解析CommandText
            //CommandType.StoredProcedure;(存储过程)
            //CommandType.TableDirect;(表 CLE DB)
            //CommandType.Text;(Sql语句)
            command.CommandType = CommandType.Text;
            command.CommandText = str;
            //Console.WriteLine(command.ExecuteScalar());
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader[0].ToString() + "\t" + reader[1].ToString() + "\t" + reader[2].ToString() + "\t" + reader[3].ToString());
            }
            connection.Close();
        }

猜你喜欢

转载自blog.csdn.net/Yayou_com/article/details/81332474
今日推荐