一些基础知识using System;
一些基础知识
using System.Collections.Generic;
一些基础知识
using System.Text;
一些基础知识
using System.IO;
一些基础知识
using System.Runtime.Serialization;
一些基础知识
using System.Runtime.Serialization.Formatters.Binary;
一些基础知识
using System.Xml.Serialization;
一些基础知识
一些基础知识
一些基础知识
namespace 基础知识

 

 

 基础知识
{
    class Program
    {        
        
private static string ConStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\学习笔记\基础知识\DB.mdf;Integrated Security=True;User Instance=True";
        
static void Main(string[] args)
        {
            DataSetTest();
            
//LinqTest();
            Console.ReadLine();
        }
        
#region * ADO.Net
        
//执行SQL语句
        public static void DoSqlCmd(string sCmdText, Dictionary<stringobject> dPar)
        {
            
using (SqlConnection con = new SqlConnection(ConStr))
            {
                con.Open();
                
using (SqlCommand cmd = new SqlCommand(sCmdText, con))
                {
                    
foreach (KeyValuePair<stringobject> kvp in dPar)
                    {
                        cmd.Parameters.AddWithValue(kvp.Key, kvp.Value);
                    }
                    cmd.ExecuteNonQuery();
                }
            }
        }
        
//
        static void DataSetTest()
        {
            
//DataSet离线数据库模型模型
            DataSet ds = new DataSet();
            DataTable dt 
= new DataTable();

            ds.Tables.Add(dt);
            DataColumn dc1 
= new DataColumn("UserID"typeof(System.Guid));
            DataColumn dc2 
= new DataColumn("UserName"typeof(System.String));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.PrimaryKey 
= new DataColumn[] { dc1 };
            DataRow dr 
= dt.NewRow();
            dr[
"UserID"= Guid.NewGuid();
            dr[
"UserName"= "a";
            dt.Rows.Add(dr);

            dt.Reset();
//重置
            SqlDataAdapter sda = new SqlDataAdapter("select * from tbUserInfo", ConStr);
            sda.Fill(dt);
            SqlCommand cmd 
= new SqlCommand("insert into tbUserInfo (UserName, Pwd, AddTime) values (@UserName, @Pwd, @AddTime)");
            cmd.Parameters.AddWithValue(
"@UserName""ff");
            cmd.Parameters.AddWithValue(
"@Pwd""ff");
            cmd.Parameters.AddWithValue(
"@AddTime", DateTime.Now);
            sda.InsertCommand 
= cmd;

            SqlCommandBuilder scb 
= new SqlCommandBuilder(sda);
            sda.Update(dt);

            
//列遍历
            foreach (DataColumn dc in dt.Columns)
            {
                Console.WriteLine(dc.ColumnName);
            }
            
//行遍历
            foreach (DataRow dr1 in dt.Rows)
            {
                Console.WriteLine(dr1[
"UserID"+ " " + dr1["UserName"]);
            }
        }
        
#endregion

        
#region * Linq to SQL为一个ORM框架(对象,关系,映射)
        
static void LinqTest()
        {
            DataContext dc 
= new DataContext(ConStr);
            dc.Log 
= Console.Out;
            Table
<tbUserInfo> Users = dc.GetTable<tbUserInfo>();//查询所有记录
            
//var Users = from user in dc.GetTable<tbUserInfo>() where user.UserName == "a" select user;
            
            
//添加
            tbUserInfo u1 = new tbUserInfo();
            u1.UserName 
= "d";
            u1.Pwd 
= "d";
            u1.AddTime 
= DateTime.Now;
            u1.Note 
= "d";
            Users.InsertOnSubmit(u1);
            
            
//先查出来再操作
            var u2 = (from user in Users where user.UserName == "d" select user).First();
            u2.Pwd 
= "test";//更新
            Users.DeleteOnSubmit(u2); //删除
           
            dc.SubmitChanges();
//提交更改,类似Dataset
            foreach (var user in Users)
            {
                Console.WriteLine(user.UserID 
+ "****" + user.UserName + user.Pwd + user.AddTime);
            }
        }
        
#endregion
    }
    
//Linq采用侵入式映射
    [Table(Name = "tbUserInfo")]
    
public class tbUserInfo
    {
        [Column(IsPrimaryKey 
= true, IsDbGenerated = true)]
        
public Guid UserID { getset; }
        [Column]
        
public string UserName { getset; }
        [Column]
        
public string Pwd { getset; }
        [Column]
        
public DateTime AddTime { getset; }
        [Column]
        
public string Note { getset; }
    }
}

 

 

 基础知识
{
    class Program
    {
        
static void Main(string[] args)
        {
            Son_Class son 
= new Son_Class();
            Console.WriteLine(Son_Class.str);
            son.virtualMethod();
            son.abstractMethod();

            myStruct s1 
= new myStruct();//直接调用构造函数,推荐
            s1.Call();
            myStruct s2;
//这种方式必须要给所有参数赋值后才能调用内部方法
            s2.Name = "hb";
            s2.Call();

            TypeChange();
            enmu_Test();

            Delegate_Test();
            Console.Read();
        }

        
static void TypeChange()
        {
            
string str = new string(new char[]{'1','2'});//提倡这样干以充分利用其构造函数,呵呵
            int i = int.Parse(str);//各种基类型都提供了parse这个对string转换直通车
            int j = Convert.ToInt32(str);//Convert用于各种基类型的强制转换
            Console.WriteLine(i.GetType());
            Son_Class son 
= new Son_Class();
            
if (son is Father_Class) 
            {
                
//is用于类型判断,无异常;as先判断然后转换(失败会NullReferenceException)
                Father_Class father = son as Father_Class;
                father 
= (Son_Class)son;
            }
        }
        
/*
         * const = ststic readonly:是编译时常量,只能为基本类型(int,string等)且必需赋初值;readonly:运行时常量,任意类型
         * 值类型深拷贝,引用类型用memberwithclone方法浅拷贝(只copy引用地址)
         
*/
        
enum Enum_famliy
        { 
            farther 
= 5,//enum是引用类型,是个抽象类,默认从0开始的常数集合
            mother,
            me
        }
        
static void enmu_Test()
        {
            Enum_famliy myFamliy 
= new Enum_famliy();
            
            
foreach(string item in Enum.GetNames(myFamliy.GetType()))
            {Console.WriteLine(item);}
//
            foreach (Enum_famliy item in Enum.GetValues(myFamliy.GetType()))
            { Console.WriteLine(item.ToString(
"D")); }//获取整数
            
        }

        
delegate void CountDelegate(int x, int y);//第一步:声明委托
        public static void Sum(int x, int y) { Console.WriteLine(x + y); }//第二步:创建关联的方法,同参同返回值
        public static void Subtract(int x, int y) { Console.WriteLine(x - y); }
        
private static CountDelegate Sum_Delegate;//第三步:定义委托类型变量
        static void Delegate_Test()
        {
            Sum_Delegate 
= new CountDelegate(Subtract);
            Sum_Delegate 
+= new CountDelegate(Sum);//委托链
            Sum_Delegate(23);
            
//匿名方法:不知道委托类型变量时只能用匿名方法
            
//CountDelegate Sum = delegate(int x, int y)//
            
//{ Console.WriteLine(x + y); };
            
//Sum(1, 2);
        }
    }
    
abstract class Father_Class //抽象类才能有抽象方法
    {
        
public static string str = "static = share,由类访问并被类的所有实例共享,避免冗余,如sqlconstr";
        
public virtual void virtualMethod()
        { Console.WriteLine(
"接口用于定义类的能力;抽象类是能提供部分实现的接口,多用于定义属性,如版本控制"); }
        
public abstract void abstractMethod();
    }
    
class Son_Class : Father_Class
    {
        
public override void virtualMethod()
        { Console.WriteLine(
"virtualMethod可以自己实现,也可以由子类override"); }
        
public override void abstractMethod()
        { Console.WriteLine(
"abstractMethod只能由子类override"); }
    }
    
struct myStruct 
    {
        
public string Name;
        
public void Call() { Console.WriteLine("Struct:值类型意味高效率;仅用于存储数据;仅可继承自接口"); }
    }

    [Serializable]
    
public class MyException : Exception, ISerializable
    {
        
//自定义本地文本信息
        private string myMsg;
        
public string MyMsg{get { return myMsg; }}
        
//重写只读本地文本信息属性
        public override string Message
        {
            
get
            {
                
string msgBase = base.Message;
                
return myMsg == null ? msgBase : msgBase + myMsg;
            }
        }

        
//实现基类的3个公有构造函数
        public MyException()
            : 
base() { }

        
public MyException(string message)
            : 
base(message) { }

        
public MyException(string message, Exception innerException)
            : 
base(message, innerException) { }

        
//为新增字段实现构造函数
        public MyException(string message, string myMsg)
            : 
this(message)
        {
            
this.myMsg = myMsg;
        }

        
public MyException(string message, string myMsg, Exception innerException)
            : 
this(message, innerException)
        {
            
this.myMsg = myMsg;
        }

        
//用于序列化的构造函数,以支持跨应用程序域或远程边界的封送处理
        protected MyException(SerializationInfo info, StreamingContext context)
            : 
base(info, context)
        {
            myMsg 
= info.GetString("MyMsg");
        }

        
//重写基类GetObjectData方法,实现向SerializationInfo中添加自定义字段信息
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(
"MyMsg", myMsg);
            
base.GetObjectData(info, context);
        }
    }


}

相关文章:

  • 2021-12-24
  • 2021-08-23
  • 2021-06-27
  • 2021-08-21
  • 2022-01-09
  • 2021-04-07
  • 2021-07-09
  • 2021-08-11
猜你喜欢
  • 2021-07-19
  • 2021-09-04
  • 2021-06-27
  • 2021-06-09
  • 2021-12-30
  • 2022-12-23
  • 2021-11-22
相关资源
相似解决方案