顯示具有 DataTable 標籤的文章。 顯示所有文章
顯示具有 DataTable 標籤的文章。 顯示所有文章

2019年9月20日

MySQL、MSSQL 連線方式及如何使用DataTable取得資料 for C#


    using MySql.Data.MySqlClient;
    using System.Data;


    DataTable dt = new DataTable();
    MySqlConnection conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MISConnection"].ConnectionString);
    conn.Open();

          using (MySqlCommand cmd = new MySqlCommand())
          {
               string CmdText = @"select * from basic where PK=@PK";

               cmd.Parameters.AddWithValue("@PK", PK);
               cmd.Connection = conn;
               cmd.CommandText = CmdText;
               
               MySqlDataReader myData = cmd.ExecuteReader();

               if (myData.HasRows)
               {
                    dt.Load(myData);
               }
          }

    conn.Close();

      
      using System.Data;
      using System.Data.SqlClient;

      SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString);
      cn.Open();
      String PK = "1";
      DataTable dt = new DataTable();
 
      string _sql = @"select * from basic where PK=@PK";
      using (SqlCommand cmd = new SqlCommand(_sql, cn))
      {
           cmd.Parameters.Clear();
           cmd.Parameters.AddWithValue("@PK", PK);
           //cmd.ExecuteNonQuery();
           using (SqlDataAdapter da = new SqlDataAdapter(cmd))
           {
               dt.BeginLoadData();
               da.Fill(dt);
           }

      }
        
        cn.Close();

2019年9月17日

C# 如何 Excel 轉成 DataTable ?

在同一資料夾下,可以這樣讀取 .xls

    DataTable dt = new DataTable();
    string filename = Server.MapPath(@"test.xls");
    dt = ConvertExcelToDataTable(filename);

    public static DataTable ConvertExcelToDataTable(string FileName)
    {
        System.Data.DataTable dtResult = null;
        int totalSheet = 0; //No of sheets on excel file  
        using (OleDbConnection objConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;';"))
        {
            objConn.Open();
            OleDbCommand cmd = new OleDbCommand();
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            DataSet ds = new DataSet();
            System.Data.DataTable dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string sheetName = string.Empty;
            if (dt != null)
            {
                var tempDataTable = (from dataRow in dt.AsEnumerable()
                                     where !dataRow["TABLE_NAME"].ToString().Contains("FilterDatabase")
                                     select dataRow).CopyToDataTable();
                dt = tempDataTable;
                totalSheet = dt.Rows.Count;
                sheetName = dt.Rows[0]["TABLE_NAME"].ToString();
            }
            cmd.Connection = objConn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM [" + sheetName + "]";
            oleda = new OleDbDataAdapter(cmd);
            oleda.Fill(ds, "excelData");
            dtResult = ds.Tables["excelData"];
            objConn.Close();
            return dtResult; //Returning Dattable  
        }
    }

2014年5月3日

DataTable 轉 Json,利用ashx回傳入HTML5 C#

在寫 HTML5 時,因為,需要利用 ashx 回傳 Json 字串,但如果自已寫一個 function 是不是太不方便了?

ASP.NET,其實有一個 Newtonsoft.Json.dll 可以使用,但您的VS 2010、2012也許是沒有這個dll檔案,但我建立可以利用 NuGet 來下載

但NuGet又是如何安裝?我覺得可以參考以下的連結

http://blog.miniasp.com/post/2011/05/17/Useful-Visual-Studio-2010-tool-NuGet-Package-Manager.aspx

安裝完後,可以打開 工具 >> NuGet 套件管理員 >> 套件管理器主控台



打開「主控台」後,會出現下圖:



請在 PM> 後面輸入

Install-Package Newtonsoft.Json

這樣您的 Bin 就會自動加入 Newtonsoft.Json.dll

接下來,就可以利用下面的範例:回傳至 HTML5 Client 端

using System;
using System.Web;

/*Json.NET相關的命名空間,請使用NuGet*/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class GetKS_TOWN : IHttpHandler {
 
    public void ProcessRequest (HttpContext context) {
        DBTools dbtools = new DBTools();
        System.Data.DataTable getLandName = dbtools.GetKS_TOWN();

        //將DataTable轉成JSON字串
        string str_json = JsonConvert.SerializeObject(getLandName, Formatting.Indented);

        context.Response.ContentType = "text/plain";
        context.Response.Write(str_json);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

以上,試試看吧!