Advertisement
Guest User

C#_JAN_1

a guest
Dec 13th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.OleDb;
  11.  
  12. namespace TestDatabaseApp_5
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.  
  20.         }
  21.         private DataTable dt = new DataTable();
  22.         private int cnt = 0;
  23.         private DataSet ds = new DataSet();
  24.  
  25.         private void Btn1_Click(object sender, EventArgs e)
  26.         {
  27.  
  28.         }
  29.  
  30.         private void Form1_Load(object sender, EventArgs e)
  31.         {
  32.             this.ActiveControl = this.txtBox1;
  33.  
  34.  
  35.         }
  36.  
  37.         private void txtBox1_TextChanged(object sender, EventArgs e)
  38.         {
  39.             OleDbConnection conn = new OleDbConnection();
  40.             OleDbCommand comm = new OleDbCommand();
  41.             long LJAN;
  42.             LJAN = Convert.ToInt64(txtBox1.Text);
  43.             LJAN = long.Parse(txtBox1.Text);
  44.             int len;
  45.             len = (LJAN == 0) ? 1 : ((int)Math.Log10(LJAN) + 1);
  46.  
  47.             conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user2\Documents\Documents\TestData_2.accdb"; // MDB名など
  48.             dataGridView1.AutoGenerateColumns = false;
  49.             DataGridViewColumn column = new DataGridViewTextBoxColumn();
  50.             // 接続します。
  51.             conn.Open();
  52.  
  53.             // SELECT文を設定します。
  54.             comm.CommandText = "SELECT * FROM test_data";
  55.             comm.Connection = conn;
  56.             OleDbDataAdapter oda = new OleDbDataAdapter();
  57.             oda.SelectCommand = comm;
  58.             // 結果を表示します。
  59.             //DataSet ds = new DataSet();
  60.             oda.Fill(ds, "data");
  61.             DataView dv = new DataView();
  62.             if (len == 13)
  63.             {
  64.  
  65.                 dv = new DataView(ds.Tables[0], "id=" + LJAN, null, DataViewRowState.CurrentRows);
  66.                 cnt += 1;
  67.  
  68.                 dataGridView1.DataSource = dv;
  69.  
  70.                 dataGridView1.Columns[3].DataPropertyName = "price";
  71.                 dataGridView1.Columns[1].DataPropertyName = "name";
  72.  
  73.                 // 接続を解除します。
  74.                 DataRowView dtrv = dv.AddNew();
  75.  
  76.                 conn.Close();
  77.  
  78.                 //dataGridView1.DataSource = null;
  79.             }
  80.             if (len >= 13)
  81.             {
  82.                 txtBox1.SelectAll();
  83.             }
  84.         }
  85.  
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement