初学C#过程中的记录的一些认为以后有用的东西

数组:
using System;
class Test
{
   static void Main()
   {
     int[] tmep = new int[5];
     for (int i = 0; i<temp.Length; i++)
       Console.WriteLine("tem[{0}] = {1}", i, tem[i]);
  
}}

引用传参:
using System;
class Test
{
 static void Swap(ref int a, ref int b){
  int t = a;
  a = b;
  b = t;
 }
   static void Main()
   {
     int x =1, y =2;//必须进行初始化,如果用out 就不需要
     Swap(ref x, ref y);
   }

}
两种连接sqlserver的方法

SQLConnection
String connectionString = "server=localhost;uid=sa;pwd=;database=northwind";
SQLConnection myConn=new SQLConnection(connectionString);
myConn.Open;

ADOConnection
String connectionString = "Provider=SQLOLEDB.1;Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind";
ADOConnection myConn=new ADOConnection(connectionString);
myConn.Open;

访问Access数据库

将此行添加到 WebForm1.aspx.cs 开始处的 using 语句  using System.Data.OleDb; 

将此代码插入到 Page_Load 方法:

     private void Page_Load(object sender, System.EventArgs e)

     {

          if (!IsPostBack)  ReadRecords();
    }
将 ReadRecords 方法添加到紧接在 Page_Load 方法之后的 PetForm 类:

 private void ReadRecords()

 {

     OleDbConnection conn = null;

     OleDbDataReader reader = null;

     try

     {

         conn = new OleDbConnection(

             "Provider=Microsoft.Jet.OLEDB.4.0; "  

             "Data Source="   Server.MapPath("Pets/Pets.mdb"));

         conn.Open();

 

         OleDbCommand cmd =

            new OleDbCommand("Select * FROM PetTable", conn);

         reader = cmd.ExecuteReader();

 

         datagrid.DataSource = reader;

         datagrid.DataBind();

 }

 //        catch (Exception e)

 //        {

 //            Response.Write(e.Message);

 //            Response.End();

 //        }

     finally

     {

         if (reader != null)  reader.Close();

         if (conn != null)  conn.Close();

     }

 }

 访问Excel数据库

将下面的语句添加到代码隐藏页顶部的命名空间部分之上:
        using System.Data.OleDb;
 using System.Data;

在 WebForm1.aspx.cs 中,将这些代码复制到 Page_Load 事件中:

 // Create connection string variable. Modify the "Data Source"

 // parameter as appropriate for your environment.

 String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" 

  "Data Source="   Server.MapPath("../ExcelData.xls")   ";" 

  "Extended Properties=Excel 8.0;"; 

 // Create connection object by using the preceding connection string.

 OleDbConnection objConn = new OleDbConnection(sConnectionString); 

 // Open connection with the database.

 objConn.Open();
 

 // The code to follow uses a SQL SELECT command to display the data from the worksheet.

 // Create new OleDbCommand to return data from worksheet.

 OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn);

 

 // Create new OleDbDataAdapter that is used to build a DataSet

 // based on the preceding SQL SELECT statement.

 OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

 

 // Pass the Select command to the adapter.

 objAdapter1.SelectCommand = objCmdSelect;

 

 // Create new DataSet to hold information from the worksheet.

 DataSet objDataset1 = new DataSet();

 

 // Fill the DataSet with the information from the worksheet.

 objAdapter1.Fill(objDataset1, "XLData");

 

 // Bind data to DataGrid control.

 DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;

 DataGrid1.DataBind();

 

 // Clean up objects.

 objConn.Close();

 

 

 

原文地址:https://www.cnblogs.com/xiachong/p/1074642.html