/* public void CodeGen() * { * SqlConnection con3 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\StockManagement.mdf;Integrated Security=True"); * * con3.Open(); * * SqlCommand cmd2 = new SqlCommand(@"SELECT Code FROM [dbo].[StockAdd]", con3); * * SqlDataReader dr = cmd2.ExecuteReader(); * * while (dr.Read()) * { * TextBox7.Text = dr.GetValue(0).ToString(); * MessageBox.Show("CODE EXECUTING!!!"); * } * * con3.Close(); * }*/ private void LoadItem() { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\StockManagement.mdf;Integrated Security=True"); SqlConnection con1 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\StockManagement.mdf;Integrated Security=True"); SqlConnection con2 = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\StockManagement.mdf;Integrated Security=True"); con2.Open(); string sql = "SELECT DISTINCT Code FROM ItemWiseRep"; SqlCommand cmd = new SqlCommand(sql, con); SqlDataReader myreader; con.Open(); myreader = cmd.ExecuteReader(); while (myreader.Read()) { string Code = myreader.GetInt32(0).ToString(); #region ADDING SqlDataAdapter sda = new SqlDataAdapter("SELECT Code,PurchaseQty,InvNo,SaleQty,[Balance Stock],NUM FROM [dbo].[ItemWiseRep] WHERE Code = '" + Code + "'", con1); DataTable dt = new DataTable(); sda.Fill(dt); DataGridView2.DataSource = dt; DataGridView2.Sort(DataGridView2.Columns[5], ListSortDirection.Ascending); for (int i = 1; i <= DataGridView2.Rows.Count; i++) { if (i > 1) { if (DataGridView2.Rows[i - 1].Cells[1].Value.ToString() != "") { int S = Convert.ToInt32(DataGridView2.Rows[i - 1].Cells[1].Value) + Convert.ToInt32(DataGridView2.Rows[i - 2].Cells[4].Value); // MessageBox.Show(String.Format("{0} + {1} = {2}", DataGridView2.Rows[i - 1].Cells[1].Value, DataGridView2.Rows[i - 2].Cells[4].Value, S)); SqlCommand cmd1 = new SqlCommand(@"UPDATE [dbo].[ItemWiseRep] SET [Balance Stock] = '" + S + "' WHERE[Code] = '" + Code + "' AND [NUM] LIKE '" + i + "'", con2); cmd1.ExecuteNonQuery(); } else if (DataGridView2.Rows[i - 1].Cells[3].Value.ToString() != "") { int S = Convert.ToInt32(DataGridView2.Rows[i - 2].Cells[4].Value) - Convert.ToInt32(DataGridView2.Rows[i - 1].Cells[3].Value); // MessageBox.Show(String.Format("{0} - {1} = {2}",Convert.ToInt32(DataGridView2.Rows[i - 2].Cells[4].Value), Convert.ToInt32(DataGridView2.Rows[i - 1].Cells[3].Value),S)); SqlCommand cmd1 = new SqlCommand(@"UPDATE [dbo].[ItemWiseRep] SET [Balance Stock] = '" + S + "' WHERE[Code] = '" + Code + "' AND [NUM] LIKE '" + i + "'", con2); cmd1.ExecuteNonQuery(); } else { continue; } } } #endregion } }
private void btnParseTextFiles_Click(object sender, EventArgs e) { using (TextFieldParser myReader = new TextFieldParser("test.txt")) { // 定义三种格式之各栏的宽度与分隔字符。 int[] FirstFormat = { 5, 10, -1 }; int[] SecondFormat = { 6, 10, 17, -1 }; string[] ThirdFormat = { "," }; this.DataGridView1.Rows.Clear(); this.DataGridView2.Rows.Clear(); this.DataGridView3.Rows.Clear(); string[] CurrentRow; while (!myReader.EndOfData) { try { string RowType = myReader.PeekChars(2); switch (RowType) { case "CK": myReader.TextFieldType = FieldType.FixedWidth; myReader.FieldWidths = FirstFormat; // 或是 myReader.SetFieldWidths(FirstFormat); CurrentRow = myReader.ReadFields(); this.DataGridView1.Rows.Add(CurrentRow); break; case "PB": myReader.TextFieldType = FieldType.FixedWidth; myReader.FieldWidths = SecondFormat; // 或是 myReader.SetFieldWidths(SecondFormat); CurrentRow = myReader.ReadFields(); this.DataGridView2.Rows.Add(CurrentRow); break; case "SP": myReader.TextFieldType = FieldType.Delimited; myReader.Delimiters = ThirdFormat; // 或是 myReader.SetDelimiters(ThirdFormat); CurrentRow = myReader.ReadFields(); this.DataGridView3.Rows.Add(CurrentRow); break; } } catch (MalformedLineException ex) { MessageBox.Show("行 " + ex.Message + " 是无效的。略过。"); } } // 排序各个 DataGridView 控件的内容。 DataGridView1.Sort(DataGridView1.Columns[0], System.ComponentModel.ListSortDirection.Ascending); DataGridView2.Sort(DataGridView2.Columns[0], System.ComponentModel.ListSortDirection.Ascending); DataGridView3.Sort(DataGridView3.Columns[0], System.ComponentModel.ListSortDirection.Ascending); } }