Form1.cs

本文介绍了一个使用C#和SQL进行用户登录验证的示例代码,同时展示了如何从数据库中读取用户信息并将其显示在dataGridView控件上。通过SqlParameter参数化查询,提高了SQL查询的安全性。

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 _001
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select count(*) from tbusers where loginid=@loginid and loginpwd=@loginpwd";
            SqlParameter[] pms = new SqlParameter[]{
            new SqlParameter("@loginid",SqlDbType.VarChar,50){Value=textBox1.Text.Trim()},
            new SqlParameter("@loginpwd",SqlDbType.VarChar,50){Value=textBox2.Text.Trim()}
            };
            int i=(int)SqlHelper.ExecuteScalar(sql,pms);
            if(i>0)
            {
            this.Text="成功";
            }
            else
            {
            this.Text="失败";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //声明集合
            List<tbusers> list= new List<tbusers>();
            string sql="select loginid,loginpwd from tbusers";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(sql))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        //添加到数据模型
                        tbusers model = new tbusers();
                        model.loginid = reader.GetString(0);
                        model.loginpwd = reader.GetString(1);

                        //把数据添加到集合里
                        list.Add(model);
                    }
                }
            }
            //将集合的数据绑定到dataGridView控件
            dataGridView1.DataSource = list;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

转载于:https://www.cnblogs.com/HarryChis/p/10368758.html

好的,我已收到你的更正要求: ✅ **不合并 `Form1.cs` 和 `Form1.Designer.cs`** ✅ 提供 **分文件的完整代码** ✅ 详细说明如何在 Visual Studio 中 **替换三个核心文件内容** --- ## ✅ 第一步:创建项目(图文式文字指南) ### 1. 打开 Visual Studio - 启动 Visual Studio(推荐 VS2019 或 VS2022) - 点击 **“创建新项目”** ### 2. 选择模板 - 在搜索框中输入:`Windows Forms App (.NET Framework)` - 选择该模板 → 点击 **下一步** ### 3. 配置项目 - 项目名称:`SimpleCompiler` - 位置:可自定义(如 `C:\MyProjects\SimpleCompiler`) - 解决方案名称:默认即可 - 框架:选择 `.NET Framework 4.7.2` 或更高版本(确保兼容性) - 点击 **创建** > ⚠️ 注意:必须选择 **“.NET Framework”** 版本,不是 .NET Core/.NET 5+。 --- ## ✅ 第二步:项目结构自动生成 创建后,Visual Studio 自动生成以下文件: ``` SimpleCompiler/ │ ├── SimpleCompiler.csproj ← 项目配置文件 ├── Form1.cs ← 主窗体逻辑代码(我们替换) ├── Form1.Designer.cs ← 窗体设计器代码(我们替换) ├── Form1.resx ← 资源文件(图标、字符串等,保留不动) └── Program.cs ← 应用入口点(我们替换) ``` 你现在要做的就是 **替换 `Form1.cs`、`Form1.Designer.cs` 和 `Program.cs` 的内容**。 --- ## ✅ 第三步:分别替换三个文件的内容 > 📌 操作方式: > - 在右侧 **“解决方案资源管理器”** 中找到对应文件 > - 双击打开 > - 删除原有全部代码 > - 将下面提供的代码 **完整复制粘贴进去** > - 保存(Ctrl + S) --- ### 🔹 文件 1:`Program.cs`(应用入口) 📌 **操作步骤**: 1. 在“解决方案资源管理器”中找到 `Program.cs` 2. 双击打开 3. 删除所有内容 4. 粘贴以下代码 ```csharp using System; using System.Windows.Forms; namespace SimpleCompiler { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } ``` ✅ 保存文件。 --- ### 🔹 文件 2:`Form1.Designer.cs`(窗体界面设计代码) 📌 **操作步骤**: 1. 在“解决方案资源管理器”中找到 `Form1.Designer.cs` 2. 双击打开 3. 删除所有内容 4. 粘贴以下代码 > ⚠️ 这是自动生成的设计器代码,我们手动写是为了绕过设计器拖拽,直接构建布局。 ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace SimpleCompiler { partial class Form1 { private RadioButton rbLexical; private RadioButton rbSyntax; private RadioButton rbSemanticAst; private RadioButton rbSemanticTac; private TextBox txtSource; private TextBox txtResult; private Button btnCompile; private GroupBox groupBox1; private Label label1; private Label label2; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,则为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.txtSource = new System.Windows.Forms.TextBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.rbSemanticTac = new System.Windows.Forms.RadioButton(); this.rbSemanticAst = new System.Windows.Forms.RadioButton(); this.rbSyntax = new System.Windows.Forms.RadioButton(); this.rbLexical = new System.Windows.Forms.RadioButton(); this.btnCompile = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.txtResult = new System.Windows.Forms.TextBox(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(20, 20); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(80, 15); this.label1.TabIndex = 0; this.label1.Text = "请输入源代码:"; // // txtSource // this.txtSource.AcceptsReturn = true; this.txtSource.AcceptsTab = true; this.txtSource.Font = new System.Drawing.Font("Courier New", 10F); this.txtSource.Location = new System.Drawing.Point(20, 50); this.txtSource.Multiline = true; this.txtSource.Name = "txtSource"; this.txtSource.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtSource.Size = new System.Drawing.Size(740, 200); this.txtSource.TabIndex = 1; this.txtSource.WordWrap = false; // // groupBox1 // this.groupBox1.Controls.Add(this.rbSemanticTac); this.groupBox1.Controls.Add(this.rbSemanticAst); this.groupBox1.Controls.Add(this.rbSyntax); this.groupBox1.Controls.Add(this.rbLexical); this.groupBox1.Location = new System.Drawing.Point(20, 260); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(740, 50); this.groupBox1.TabIndex = 2; this.groupBox1.TabStop = false; this.groupBox1.Text = "选择分析阶段"; // // rbSemanticTac // this.rbSemanticTac.AutoSize = true; this.rbSemanticTac.Location = new System.Drawing.Point(500, 20); this.rbSemanticTac.Name = "rbSemanticTac"; this.rbSemanticTac.Size = new System.Drawing.Size(133, 19); this.rbSemanticTac.TabIndex = 3; this.rbSemanticTac.TabStop = true; this.rbSemanticTac.Text = "语义分析(TAC)"; this.rbSemanticTac.UseVisualStyleBackColor = true; // // rbSemanticAst // this.rbSemanticAst.AutoSize = true; this.rbSemanticAst.Location = new System.Drawing.Point(340, 20); this.rbSemanticAst.Name = "rbSemanticAst"; this.rbSemanticAst.Size = new System.Drawing.Size(133, 19); this.rbSemanticAst.TabIndex = 2; this.rbSemanticAst.Text = "语义分析(AST)"; this.rbSemanticAst.UseVisualStyleBackColor = true; // // rbSyntax // this.rbSyntax.AutoSize = true; this.rbSyntax.Location = new System.Drawing.Point(180, 20); this.rbSyntax.Name = "rbSyntax"; this.rbSyntax.Size = new System.Drawing.Size(88, 19); this.rbSyntax.TabIndex = 1; this.rbSyntax.Text = "语法分析"; this.rbSyntax.UseVisualStyleBackColor = true; // // rbLexical // this.rbLexical.AutoSize = true; this.rbLexical.Checked = true; this.rbLexical.Location = new System.Drawing.Point(20, 20); this.rbLexical.Name = "rbLexical"; this.rbLexical.Size = new System.Drawing.Size(88, 19); this.rbLexical.TabIndex = 0; this.rbLexical.TabStop = true; this.rbLexical.Text = "词法分析"; this.rbLexical.UseVisualStyleBackColor = true; // // btnCompile // this.btnCompile.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold); this.btnCompile.Location = new System.Drawing.Point(680, 270); this.btnCompile.Name = "btnCompile"; this.btnCompile.Size = new System.Drawing.Size(80, 30); this.btnCompile.TabIndex = 3; this.btnCompile.Text = "编 译"; this.btnCompile.UseVisualStyleBackColor = true; this.btnCompile.Click += new System.EventHandler(this.BtnCompile_Click); // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(20, 320); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(56, 15); this.label2.TabIndex = 4; this.label2.Text = "分析结果:"; // // txtResult // this.txtResult.Font = new System.Drawing.Font("Courier New", 10F); this.txtResult.Location = new System.Drawing.Point(20, 350); this.txtResult.Multiline = true; this.txtResult.Name = "txtResult"; this.txtResult.ReadOnly = true; this.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtResult.Size = new System.Drawing.Size(740, 200); this.txtResult.TabIndex = 5; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(784, 561); this.Controls.Add(this.txtResult); this.Controls.Add(this.label2); this.Controls.Add(this.btnCompile); this.Controls.Add(this.groupBox1); this.Controls.Add(this.txtSource); this.Controls.Add(this.label1); this.MaximizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "简易编译器"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion } } ``` ✅ 保存文件。 --- ### 🔹 文件 3:`Form1.cs`(主逻辑代码) 📌 **操作步骤**: 1. 在“解决方案资源管理器”中找到 `Form1.cs` 2. 双击打开 3. 删除所有内容 4. 粘贴以下代码 > ⚠️ 注意:这个文件只包含构造函数和事件处理方法,不包含设计器代码。 ```csharp using System; using System.Windows.Forms; namespace SimpleCompiler { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void BtnCompile_Click(object sender, EventArgs e) { string sourceCode = txtSource.Text.Trim(); if (string.IsNullOrEmpty(sourceCode)) { MessageBox.Show("请输入源代码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } txtResult.Clear(); if (rbLexical.Checked) { string result = LexicalAnalysis(sourceCode); txtResult.Text = result; } else if (rbSyntax.Checked) { string result = SyntaxAnalysis(sourceCode); txtResult.Text = result; } else if (rbSemanticAst.Checked) { string result = SemanticAnalysis_AST(sourceCode); txtResult.Text = result; } else if (rbSemanticTac.Checked) { string result = SemanticAnalysis_TAC(sourceCode); txtResult.Text = result; } } // ------------------ 各阶段分析方法(桩函数)------------------ private string LexicalAnalysis(string source) { return $"[词法分析]\r\n输入:\r\n{source}\r\n\r\n输出示例:\r\nTOKEN(ID, a)\r\nTOKEN(INT_CONST, 123)\r\nTOKEN(SEMICOLON, ;)"; } private string SyntaxAnalysis(string source) { return $"[语法分析]\r\n输入:\r\n{source}\r\n\r\n输出示例:\r\n语法结构正确。\r\n或:第2行附近发现语法错误。"; } private string SemanticAnalysis_AST(string source) { return $"[语义分析 - AST]\r\n输入:\r\n{source}\r\n\r\n输出示例:\r\nProgram\r\n├─ VarDecl: int a\r\n└─ Stmt: a := 5"; } private string SemanticAnalysis_TAC(string source) { return $"[语义分析 - TAC]\r\n输入:\r\n{source}\r\n\r\n输出示例:\r\n(1) ASSIGN, 5, _, a\r\n(2) ADD, a, 1, t1"; } } } ``` ✅ 保存文件。 --- ## ✅ 第四步:运行项目 - 按下 **F5** 或点击工具栏上的 **“启动”按钮** - 如果没有报错,你应该看到一个整洁的窗口: - 上方是源码输入区 - 中间是单选按钮选择分析阶段 - 下方是结果输出区 - 输入任意文本,点击【编译】,会根据选择显示对应的模拟结果 --- ## ✅ 文件职责总结 | 文件 | 职责 | |------|------| | `Program.cs` | 程序入口,启动窗体 | | `Form1.cs` | 业务逻辑(按钮点击、调用分析函数) | | `Form1.Designer.cs` | UI 控件初始化与布局(由设计器生成,我们手动提供) | > 💡 好处:分离后便于维护,将来 Designer 文件可由可视化设计器自动更新,而逻辑代码不受影响。 --- 现在你已经有了一个 **结构清晰、可运行、符合 C# 7.3 标准的完整项目框架**! 你可以随时告诉我: 👉 “请开始实现词法分析器” 👉 “请定义 Token 类” 👉 “请教我如何用 DFA 识别标识符和关键字” 我会继续带你一步步完成整个编译器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值