1. Trang chủ
  2. » Công Nghệ Thông Tin

MÃ NGUỒN mở ôn CUỐI kỳ VICTORY

17 280 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Tape9_DataBinding {

Ôn phát triển phần mềm nguồn mở *** namespace Tape9_DataBinding using using using using using using using using using using System; System.Collections; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms; namespace Tape9_DataBinding { public partial class Form2 : Form { ArrayList @arraylist; DataSet @dataset = new DataSet("MyData"); DataTable @table = new DataTable("MyTable"); public Form2() { InitializeComponent(); arraylist = new ArrayList(); arraylist.Add("S"); arraylist.Add("H"); arraylist.Add("E"); arraylist.Add("B"); @textbox.DataBindings.Add("Text", @arraylist, ""); InitData(); firstname.DataBindings.Add("Text", dataset.Tables["MyTable"], "FN"); fullname.DataBindings.Add("Text", dataset.Tables["MyTable"], "SN"); company.DataBindings.Add("Text", dataset.Tables["MyTable"], "C"); telephone.DataBindings.Add("Text", dataset.Tables["MyTable"], "Tel"); } public void InitData() { @table.Columns.Add("FN", typeof(System.String)); @table.Columns.Add("SN", typeof(System.String)); @table.Columns.Add("C", typeof(System.String)); @table.Columns.Add("Tel", typeof(System.String)); @dataset.Tables.Add(@table); } private void nextview_Click(object sender, EventArgs e) { this.BindingContext[@dataset.Tables["MyTable"]].Position++; } Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở private void newrow_Click(object sender, EventArgs e) { DataRow @row = @dataset.Tables["MyTable"].NewRow(); @row["FN"] = "Blank"; @row["SN"] = ""; @row["C"] = ""; @row["Tel"] = ""; @dataset.Tables["MyTable"].Rows.Add(@row); this.BindingContext[@dataset.Tables["MyTable"]].Position = @dataset.Tables["MyTable"].Rows.Count-1; } private void preview_Click(object sender, EventArgs e) { this.BindingContext[@dataset.Tables["MyTable"]].Position ; } private void button1_Click(object sender, EventArgs e) { this.BindingContext[@arraylist].Position++; } private void endview_Click(object sender, EventArgs e) { } } } using using using using using System; System.Collections.Generic; System.Linq; System.Threading.Tasks; System.Windows.Forms; namespace Tape9_DataBinding { static class Tape9_DataBinding { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form2()); } } } ***namespace Tape10_ConnectDataBase using System; using System.Collections.Generic; using System.Linq; Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở using using using using System.Text; System.Threading.Tasks; System.Data.OleDb; System.Windows.Forms; namespace Tape10_ConnectDataBase { class DB { OleDbConnection conect; OleDbCommand cmd; /* * SqlConnection cnn = new SqlConnection(); * cnn.ConnectionString = “Data Source=MC-PC;Initial Catalog=master;Integrated Security=True”; */ public DB() { conect = new OleDbConnection(); string s= @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Administrator\Desktop\Database1.mdb"; conect.ConnectionString = s; conect.Open(); cmd = new OleDbCommand(); cmd.Connection = conect; cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "select COUNT(*) from DICHVU"; conect.Close(); Console.ReadKey(); } } } using using using using using System; System.Collections.Generic; System.Linq; System.Threading.Tasks; System.Windows.Forms; namespace Tape10_ConnectDataBase { static class Program { [STAThread] static void Main() { DB db = new DB(); } } } ***namespace Tape11_ConnectDataBase_withForm Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở using using using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient; System.Configuration; System.Data.OleDb; namespace Tape11_ConnectDataBase_withForm { public partial class Form1 : Form { OleDbCommand cmd; OleDbConnection conn; string connetionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\Administrator\Desktop\Database1.mdb"; public Form1() { InitializeComponent(); } /* * Kết nối xử lý CSDL MS SQL Server thực bước * step 1: Khởi tạo chuổi kết nối(Connetion) * Server DB: Express, Enterprise * Athertication: Window, SqlServer * Database: Initial Catalog: vidu * !! @ * step 2: mỡ kết nối * step 3: khởi tạo lệnh (Command) * tao doi tuong lenh: truc tiep hoac gian tiep(Stored Procedures) * * step 4: Xử lý lưu liệu tra * ADO.Net: truy van tra ve tap gia tri (selecte) * ADO.NET: Truy van khong tra ve tap gia tri * step 5: trình diễn liệu * step 6: đóng kết nối * !! Why? colose connection * !! Why? try catch finally * !! App.Config * */ private void btConnetion_Click(object sender, EventArgs e) { conn = new OleDbConnection(); conn.ConnectionString = connetionString; try { conn.Open(); Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở MessageBox.Show("Ket noi cong", "Thanh Cong", MessageBoxButtons.OK, MessageBoxIcon.Information); cmd.Connection = conn; cmd.CommandType = System.Data.CommandType.Text; } catch(Exception ex) { conn.Close(); conn.Dispose(); } finally { conn.Close(); conn.Dispose(); } } private void btShow_Click(object sender, EventArgs e) { MessageBox.Show("Ket noi cong", "Thanh Cong", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } using System; namespace Tape11_ConnectDataBase_withForm { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.btConnetion = new System.Windows.Forms.Button(); this.btShow = new System.Windows.Forms.Button(); this.tbtruyvan = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // btConnetion // this.btConnetion.Location = new System.Drawing.Point(102, 1); Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở this.btConnetion.Name = "btConnetion"; this.btConnetion.Size = new System.Drawing.Size(127, 23); this.btConnetion.TabIndex = 0; this.btConnetion.Text = "TestConnection"; this.btConnetion.UseVisualStyleBackColor = true; this.btConnetion.Click += new System.EventHandler(this.btConnetion_Click); // // btShow // this.btShow.Location = new System.Drawing.Point(279, 32); this.btShow.Name = "btShow"; this.btShow.Size = new System.Drawing.Size(75, 23); this.btShow.TabIndex = 1; this.btShow.Text = "Show"; this.btShow.UseVisualStyleBackColor = true; this.btShow.Click += new System.EventHandler(this.btShow_Click); // // tbtruyvan // this.tbtruyvan.Location = new System.Drawing.Point(93, 35); this.tbtruyvan.Name = "tbtruyvan"; this.tbtruyvan.Size = new System.Drawing.Size(165, 20); this.tbtruyvan.TabIndex = 2; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(24, 38); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(50, 13); this.label1.TabIndex = 3; this.label1.Text = "Truy Van"; // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Location = new System.Drawing.Point(27, 81); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(327, 150); this.dataGridView1.TabIndex = 4; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(366, 262); this.Controls.Add(this.dataGridView1); this.Controls.Add(this.label1); this.Controls.Add(this.tbtruyvan); this.Controls.Add(this.btShow); this.Controls.Add(this.btConnetion); this.Name = "Form1"; this.Text = "ADONET"; ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở } #endregion private private private private private System.Windows.Forms.Button btConnetion; System.Windows.Forms.Button btShow; System.Windows.Forms.TextBox tbtruyvan; System.Windows.Forms.Label label1; System.Windows.Forms.DataGridView dataGridView1; } } using using using using System; System.Collections.Generic; System.Linq; System.Windows.Forms; namespace Tape11_ConnectDataBase_withForm { static class Program { /// /// The main entry point for the application /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } Tạo form cấu hình kết nối SQL cho ứng dụng C# Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở Form Tên Server Xác thực server Tên đăng nhập Mật Kết nối Thoát Form2 DataGridView(phần kết nối với DataSource với ConnectDB) using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Data.Sql; System.Data.SqlClient; System.Drawing; Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở using System.Linq; using System.Text; using System.Windows.Forms; namespace FormConnect { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { txtUser.Enabled = false; txtPass.Enabled = false; SqlDataSourceEnumerator instance =SqlDataSourceEnumerator.Instance; DataTable table = instance.GetDataSources(); cbServer.DataSource = table; cbServer.DisplayMember = "ServerName"; } private void ckAut_CheckedChanged(object sender, EventArgs e) { if (ckAut.Checked) { txtUser.Enabled = true; txtPass.Enabled = true; } else { txtUser.Enabled = false; txtPass.Enabled = false; } } private void btnConnect_Click(object sender, EventArgs e) { string strConnect = ""; if (!ckAut.Checked) strConnect = "Server=" + cbServer.Text + ";Database=StormDevShop;Trusted_Connection=True;"; else strConnect = "Server="+cbServer.Text+ ";Database=StormDevShop;User Id="+txtUser.Text+";Password = "+txtPass.Text+"; "; SqlConnection sqlcon=new SqlConnection(strConnect); try { sqlcon.Open(); FormConnect.Properties.Settings.Default.strConnect = strConnect; FormConnect.Properties.Settings.Default.Save(); Nguyễn Tấn Cường pg Ôn phát triển phần mềm nguồn mở MessageBox.Show("Kết nối thành công, vui lòng khởi động lại chương trình sau tiếp beep!"); Console.Beep(); Application.Exit(); } catch (Exception) { MessageBox.Show("Kết nối không thành công, xin kiểm tra lại!"); sqlcon.Close(); } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } } } using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; namespace FormConnect { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { dataGridView1.DataSource = ConnectDB.getData("Select * from Demo"); } } } using using using using using using System; System.Collections.Generic; System.Data; System.Data.SqlClient; System.Linq; System.Text; namespace FormConnect { static class ConnectDB { Nguyễn Tấn Cường pg 10 Ôn phát triển phần mềm nguồn mở public static SqlConnection sqlcon = new SqlConnection(FormConnect.Properties.Settings.Default.strConnect); public static DataTable getData(string sql) { var dt = new DataTable(); var da = new SqlDataAdapter(sql, sqlcon); sqlcon.Open(); da.Fill(dt); sqlcon.Close(); return dt; } } } namespace FormConnect { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.cbServer = new System.Windows.Forms.ComboBox(); this.ckAut = new System.Windows.Forms.CheckBox(); this.txtUser = new System.Windows.Forms.TextBox(); this.txtPass = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.btnConnect = new System.Windows.Forms.Button(); this.btnExit = new System.Windows.Forms.Button(); this.SuspendLayout(); // // cbServer // this.cbServer.FormattingEnabled = true; this.cbServer.Location = new System.Drawing.Point(205, 12); this.cbServer.Name = "cbServer"; this.cbServer.Size = new System.Drawing.Size(369, 33); this.cbServer.TabIndex = 0; // // ckAut Nguyễn Tấn Cường pg 11 Ôn phát triển phần mềm nguồn mở // this.ckAut.AutoSize = true; this.ckAut.Location = new System.Drawing.Point(205, 51); this.ckAut.Name = "ckAut"; this.ckAut.Size = new System.Drawing.Size(197, 29); this.ckAut.TabIndex = 1; this.ckAut.Text = "Xác thực Server"; this.ckAut.UseVisualStyleBackColor = true; this.ckAut.CheckedChanged += new System.EventHandler(this.ckAut_CheckedChanged); // // txtUser // this.txtUser.Location = new System.Drawing.Point(205, 112); this.txtUser.Name = "txtUser"; this.txtUser.Size = new System.Drawing.Size(369, 31); this.txtUser.TabIndex = 2; // // txtPass // this.txtPass.Location = new System.Drawing.Point(205, 149); this.txtPass.Name = "txtPass"; this.txtPass.PasswordChar = '*'; this.txtPass.Size = new System.Drawing.Size(369, 31); this.txtPass.TabIndex = 3; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(41, 15); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(115, 25); this.label1.TabIndex = 4; this.label1.Text = "Tên server"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(41, 115); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(157, 25); this.label2.TabIndex = 5; this.label2.Text = "Tên đăng nhập"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(41, 155); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(101, 25); this.label3.TabIndex = 6; this.label3.Text = "Mật khẩu"; // // btnConnect // this.btnConnect.Location = new System.Drawing.Point(150, 212); Nguyễn Tấn Cường pg 12 Ôn phát triển phần mềm nguồn mở this.btnConnect.Name = "btnConnect"; this.btnConnect.Size = new System.Drawing.Size(124, 45); this.btnConnect.TabIndex = 7; this.btnConnect.Text = "Kết nối"; this.btnConnect.UseVisualStyleBackColor = true; this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click); // // btnExit // this.btnExit.Location = new System.Drawing.Point(393, 212); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(118, 45); this.btnExit.TabIndex = 8; this.btnExit.Text = "Thoát"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(619, 295); this.Controls.Add(this.btnExit); this.Controls.Add(this.btnConnect); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtPass); this.Controls.Add(this.txtUser); this.Controls.Add(this.ckAut); this.Controls.Add(this.cbServer); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private private private private private private private private private System.Windows.Forms.ComboBox cbServer; System.Windows.Forms.CheckBox ckAut; System.Windows.Forms.TextBox txtUser; System.Windows.Forms.TextBox txtPass; System.Windows.Forms.Label label1; System.Windows.Forms.Label label2; System.Windows.Forms.Label label3; System.Windows.Forms.Button btnConnect; System.Windows.Forms.Button btnExit; } } namespace FormConnect Nguyễn Tấn Cường pg 13 Ôn phát triển phần mềm nguồn mở { partial class Form2 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.dataGridView1 = new System.Windows.Forms.DataGridView(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit() ; this.SuspendLayout(); // // dataGridView1 // this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridView1.Location = new System.Drawing.Point(0, 0); this.dataGridView1.Name = "dataGridView1"; this.dataGridView1.Size = new System.Drawing.Size(1214, 742); this.dataGridView1.TabIndex = 0; // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1214, 742); this.Controls.Add(this.dataGridView1); this.Name = "Form2"; this.Text = "Form2"; this.Load += new System.EventHandler(this.Form2_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView dataGridView1; } } using System; Nguyễn Tấn Cường pg 14 Ôn phát triển phần mềm nguồn mở using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace FormConnect { static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); try { ConnectDB.sqlcon.Open(); ConnectDB.sqlcon.Close(); Application.Run(new Form2()); } catch { Application.Run(new Form1()); } } } } Tạo Form đăng nhập c# (chưa có kết nối CSDL ) Nguyễn Tấn Cường pg 15 Ôn phát triển phần mềm nguồn mở Login Account Pass Show password Login using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace LoginDemo123 { public partial class frmLogin : Form { StudentManagement st; string strConnection = @"Data Source=HP\SQLEXPRESS;Initial Catalog=Login;Integrated Security=True"; SqlConnection conn; SqlCommand command; public frmLogin() { InitializeComponent(); } private void frmLogin_Load(object sender, EventArgs e) { } private void btnLogin_Click(object sender, EventArgs e) { Try { string sql = "Select Count(*) From [Login].[dbo].[tblLogin] Where Taikhoan=@acc And Matkhau=@pass "; conn = new SqlConnection(strConnection); conn.Open(); command = new SqlCommand(sql, conn); Nguyễn Tấn Cường pg 16 Ôn phát triển phần mềm nguồn mở command.Parameters.Add(new SqlParameter("@acc", txtAccount.Text)); command.Parameters.Add(new SqlParameter("@pass", txtPassword.Text)); int x = (int)command.ExecuteScalar(); if (x == 1) {//đăng nhập thành công this.Hide(); st = new StudentManagement(); st.Show(); } Else { //đăng nhập thất bại lblIncorrect.Text = "Account Or Password Is Not Correct"; txtAccount.Text = ""; txtPassword.Text = ""; txtAccount.Focus(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void ckbShowPassword_CheckedChanged(object sender, EventArgs e) { if (ckbShowPassword.Checked) { txtPassword.UseSystemPasswordChar = false; } else { txtPassword.UseSystemPasswordChar = true; } } } } Nguyễn Tấn Cường pg 17 ... FormConnect.Properties.Settings.Default.Save(); Nguyễn Tấn Cường pg Ôn phát triển phần mềm mã nguồn mở MessageBox.Show("Kết nối thành công, vui lòng khởi động lại chương trình sau tiếp beep!"); Console.Beep();... System; using System.Collections.Generic; using System.Linq; Nguyễn Tấn Cường pg Ôn phát triển phần mềm mã nguồn mở using using using using System.Text; System.Threading.Tasks; System.Data.OleDb;... new DB(); } } } ***namespace Tape11_ConnectDataBase_withForm Nguyễn Tấn Cường pg Ôn phát triển phần mềm mã nguồn mở using using using using using using using using using using using System; System.Collections.Generic;

Ngày đăng: 08/05/2017, 12:43

Xem thêm: MÃ NGUỒN mở ôn CUỐI kỳ VICTORY

TỪ KHÓA LIÊN QUAN

w