• C#编码规范


    1、连接数据库并返回DataTable

       

    View Code

    public DataTable GetDataByTable()
    {
    DataSet dataset = new DataSet();
    DataTable datatable = null;
    SqlConnection sqlConnection = new SqlConnection("Data Source=.;Initial Catalog=Calamity_Data;Integrated Security=True");
    try
    {
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select top 50 * from calamity", sqlConnection);
    sqlDataAdapter.Fill(dataset, "calamity");
    if (dataset != null && dataset.Tables.Count > 0)
    {
    datatable = dataset.Tables[0];
    }
    }
    catch (Exception e)
    {
    }
    finally
    {
    sqlConnection.Close();
    }
    Console.WriteLine("Calling WCF Service,Transfer data using DataTable");
    return datatable;
    }

    2、连接数据库文件的相对路径

    SqlConnection sqlConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database\\DatabaseWCF.mdf;Integrated Security=True;User Instance=True");
    							

    3、数组

    交叉数组

    for实现 

    int[][] arri = new
    								int[3][];
    arri[0] = new int[] { 1, 2 };
    arri[1] = new int[] { 3 };
    arri[2] = new int[] { 4, 5, 6 };
    int rlen=arri.Length;
    for (int i=0; i < rlen; i++)
    {
    int clen = arri[i].Length;
    for (int j = 0; j < clen; j++)
    {
    Console.WriteLine(arri[i][j]);
    }
    }
    Console.Read();

    foreach实现

    foreach (int[] i in arri)
    {
    foreach (int j in i)
    {
    Console.WriteLine(j);
    }

    }
    Console.Read();

    二维数组

    View Code

    int[,] arri = new
    								int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
    int rlen = arri.GetLength(0);//行数
    int clen = arri.GetLength(1);//列数

    for (int i = 0; i < rlen; i++)
    {
    for (int j = 0; j < clen; j++)
    {
    Console.WriteLine("第{0}行{1}列,值为{2}", i, j, arri[i, j]);
    }
    }
    Console.Read();

    运行结果:

    List应用

  • 相关阅读:
    JAVA基础知识之多线程——线程通信
    为Apache配置虚拟机Virtual Host
    SignalR的坑爹细节
    ashx入侵
    aspnet5安装ef7备忘
    aspnet5备忘
    NHibernate初步使用
    MVC中发生System.Data.Entity.Validation.DbEntityValidationException验证异常的解决方法
    关于NLog的target和Layout
    泛型约束的大概模样
  • 原文地址:https://www.cnblogs.com/sjllef/p/1997531.html
Copyright © 2020-2023  润新知