当前位置:Gxlcms > 数据库问题 > 传智播客数据绑定和数据库开发基础(第四季)-杨中科

传智播客数据绑定和数据库开发基础(第四季)-杨中科

时间:2021-07-01 10:21:17 帮助过: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.Windows.Forms; 9 using System.Data.SqlClient; 10 11 namespace adonet 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void button1_Click(object sender, EventArgs e) 21 { 22 SqlHelper.ExecuteNonQuery("insert into T_Student(Name,Age) values(‘a‘,33)"); 23 DataTable table = SqlHelper.ExecuteDataTable("select * from T_Student where Age=@Age or Name=@Name", 24 new SqlParameter("@Age", 26), new SqlParameter("name", "刘洋")); 25 foreach (DataRow row in table.Rows) 26 { 27 string name = (string)row["name"]; 28 MessageBox.Show(name); 29 } 30 } 31 } 32 } View Code 技术分享
 1 namespace adonet
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// 必需的设计器变量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// 清理所有正在使用的资源。
12         /// </summary>
13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows 窗体设计器生成的代码
24 
25         /// <summary>
26         /// 设计器支持所需的方法 - 不要
27         /// 使用代码编辑器修改此方法的内容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.button1 = new System.Windows.Forms.Button();
32             this.SuspendLayout();
33             // 
34             // button1
35             // 
36             this.button1.Location = new System.Drawing.Point(88, 78);
37             this.button1.Name = "button1";
38             this.button1.Size = new System.Drawing.Size(75, 23);
39             this.button1.TabIndex = 0;
40             this.button1.Text = "button1";
41             this.button1.UseVisualStyleBackColor = true;
42             this.button1.Click += new System.EventHandler(this.button1_Click);
43             // 
44             // Form1
45             // 
46             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
47             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
48             this.ClientSize = new System.Drawing.Size(284, 261);
49             this.Controls.Add(this.button1);
50             this.Name = "Form1";
51             this.Text = "Form1";
52             this.ResumeLayout(false);
53 
54         }
55 
56         #endregion
57 
58         private System.Windows.Forms.Button button1;
59     }
60 }
View Code 技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Windows.Forms;
 5 
 6 namespace adonet
 7 {
 8     static class Program
 9     {
10         /// <summary>
11         /// 应用程序的主入口点。
12         /// </summary>
13         [STAThread]
14         static void Main()
15         {
16             Application.EnableVisualStyles();
17             Application.SetCompatibleTextRenderingDefault(false);
18             Application.Run(new Form1());
19         }
20     }
21 }
View Code

sqlhelper

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Data.SqlClient;
 6 using System.Configuration;
 7 using System.Data;
 8 
 9 namespace adonet
10 {
11     class SqlHelper
12     {
13         private static string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
14 
15         public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
16         {
17             using (SqlConnection conn = new SqlConnection(connStr))
18             {
19                 conn.Open();
20                 using (SqlCommand cmd = conn.CreateCommand())
21                 {
22                     cmd.CommandText = sql;
23                     cmd.Parameters.AddRange(parameters);
24                     return cmd.ExecuteNonQuery();
25                 }
26             }
27         }
28 
29         public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
30         {
31             using (SqlConnection conn = new SqlConnection(connStr))
32             {
33                 conn.Open();
34                 using (SqlCommand cmd = conn.CreateCommand())
35                 {
36                     cmd.CommandText = sql;
37                     cmd.Parameters.AddRange(parameters);
38                     return cmd.ExecuteScalar();
39                 }
40             }
41         }
42 
43         public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
44         {
45             using (SqlConnection conn = new SqlConnection(connStr))
46             {
47                 conn.Open();
48                 using (SqlCommand cmd = conn.CreateCommand())
49                 {
50                     cmd.CommandText = sql;
51                     cmd.Parameters.AddRange(parameters);
52 
53                     SqlDataAdapter adapter = new SqlDataAdapter(cmd);
54                     DataSet dataset=new DataSet();
55                     adapter.Fill(dataset);
56                     return dataset.Tables[0];
57                 }
58             }
59         }
60     }
61 }
View Code

 

(三)ADO.Net案例:登陆、数据导入导出、省市联动选择

技术分享
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3   <connectionStrings>
4     <add name="dbConnStr" connectionString="data source=.;initial catalog=Scott;user id=sa;password=222222"/>
5   </connectionStrings>
6 </configuration>
View Code

 

技术分享
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3   <connectionStrings>
4     <add name="myconnStr" connectionString ="data source=.;initial catalog=Scott;user id=sa;password=222222"/>
5   </connectionStrings>
6 </configuration>
View Code 技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Adonet
 7 {
 8     class Area
 9     {
10         public int AreaId
11         { get; set; }
12         public string AreaName
13         { get; set; }
14     }
15 }
View Code 技术分享
 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.Windows.Forms;
 9 using System.Data.SqlClient;
10 
11 namespace Adonet
12 {
13     public partial class LoginWindow : Form
14     {
15         public LoginWindow()
16         {
17             InitializeComponent();
18         }
19 
20         private void btnLogin_Click(object sender, EventArgs e)
21         {
22             if (userName.Text.Length <= 0)
23             {
24                 MessageBox.Show("请输入用户名");
25                 return;
26             }
27             if (passWord.Text.Length <= 0)
28             {
29                 MessageBox.Show("请输入密码");
30                 return;
31             }
32 
33             DataTable table = SqlHelper.ExecuteDataTable("select * from T_User where username=@username;", 
34                 new SqlParameter("@username", userName.Text));
35             if (table.Rows.Count <= 0)
36             {
37                 MessageBox.Show("用户名不存在");
38                 return;
39             }
40             if (table.Rows.Count > 1)
41             {
42                 MessageBox.Show("不好啦,用户名重复");
43                 return;
44             }
45             DataRow row = table.Rows[0];
46             string dbPassword = (string)row["Password"];
47             long id = (long)row["Id"];
48             int errorTimes = (int)row["ErrorTimes"];
49             if (errorTimes >= 3)
50             {
51                 MessageBox.Show("输入次数过多,用户已经锁定");
52                 return;
53             }
54             if (passWord.Text != dbPassword)
55             {
56                 SqlHelper.ExecuteNonQuery("update T_User set ErrorTimes=ErrorTimes+1 where Id=@Id",
57                     new SqlParameter("@Id", id));
58                 MessageBox.Show("密码错误");
59             }
60             else
61             {
62                 MessageBox.Show("登陆成功");
63             }
64         }
65     }
66 }
View Code 技术分享
  1 namespace Adonet
  2 {
  3     partial class LoginWindow
  4     {
  5         /// <summary>
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary>
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region Windows 窗体设计器生成的代码
 24 
 25         /// <summary>
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.userName = new System.Windows.Forms.TextBox();
 32             this.label1 = new System.Windows.Forms.Label();
 33             this.label2 = new System.Windows.Forms.Label();
 34             this.passWord = new System.Windows.Forms.TextBox();
 35             this.btnLogin = new System.Windows.Forms.Button();
 36             this.SuspendLayout();
 37             // 
 38             // userName
 39             // 
 40             this.userName.Location = new System.Drawing.Point(135, 34);
 41             this.userName.Name = "userName";
 42             this.userName.Size = new System.Drawing.Size(100, 21);
 43             this.userName.TabIndex = 0;
 44             // 
 45             // label1
 46             // 
 47             this.label1.AutoSize = true;
 48             this.label1.Location = new System.Drawing.Point(46, 37);
 49             this.label1.Name = "label1";
 50             this.label1.Size = new System.Drawing.Size(41, 12);
 51             this.label1.TabIndex = 1;
 52             this.label1.Text = "用户名";
 53             // 
 54             // label2
 55             // 
 56             this.label2.AutoSize = true;
 57             this.label2.Location = new System.Drawing.Point(58, 104);
 58             this.label2.Name = "label2";
 59             this.label2.Size = new System.Drawing.Size(29, 12);
 60             this.label2.TabIndex = 2;
 61             this.label2.Text = "密码";
 62             // 
 63             // passWord
 64             // 
 65             this.passWord.Location = new System.Drawing.Point(135, 101);
 66             this.passWord.Name = "passWord";
 67             this.passWord.Size = new System.Drawing.Size(100, 21);
 68             this.passWord.TabIndex = 3;
 69             // 
 70             // btnLogin
 71             // 
 72             this.btnLogin.Location = new System.Drawing.Point(88, 161);
 73             this.btnLogin.Name = "btnLogin";
 74             this.btnLogin.Size = new System.Drawing.Size(75, 23);
 75             this.btnLogin.TabIndex = 4;
 76             this.btnLogin.Text = "登录";
 77             this.btnLogin.UseVisualStyleBackColor = true;
 78             this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
 79             // 
 80             // LoginWindow
 81             // 
 82             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
 83             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
 84             this.ClientSize = new System.Drawing.Size(284, 261);
 85             this.Controls.Add(this.btnLogin);
 86             this.Controls.Add(this.passWord);
 87             this.Controls.Add(this.label2);
 88             this.Controls.Add(this.label1);
 89             this.Controls.Add(this.userName);
 90             this.Name = "LoginWindow";
 91             this.Text = "LoginWindow";
 92             this.ResumeLayout(false);
 93             this.PerformLayout();
 94 
 95         }
 96 
 97         #endregion
 98 
 99         private System.Windows.Forms.TextBox userName;
100         private System.Windows.Forms.Label label1;
101         private System.Windows.Forms.Label label2;
102         private System.Windows.Forms.TextBox passWord;
103         private System.Windows.Forms.Button btnLogin;
104     }
105 }
View Code 技术分享
 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.Windows.Forms;
 9 using System.Data.SqlClient;
10 
11 namespace Adonet
12 {
13     public partial class CitySelectWindow : Form
14     {
15         public CitySelectWindow()
16         {
17             InitializeComponent();
18         }
19 
20         Dictionary<string, int> dictProv = new Dictionary<string, int>();
21         private void CitySelectWindow_Load(object sender, EventArgs e)
22         {
23             DataTable dtProv = SqlHelper.ExecuteDataTable("select * from AreaFull where AreaPid=0");
24             //List<Area> listProv = new List<Area>();
25             //Area area = new Area();
26             foreach (DataRow row in dtProv.Rows)
27             {
28                 //area.AreaId = (int)row["AreaId"];
29                 //area.AreaName = (string)row["AreaName"];

                        
                    

人气教程排行