问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

C# WinForms

发布网友 发布时间:2022-04-10 17:21

我来回答

2个回答

热心网友 时间:2022-04-10 18:50

花了几分钟写的,没有进行代码优化,不过你说的功能都实现了。`
请先在数据库中添加一个名字为test的数据库,一个test的表
表中的字段为:id,name,address
数据库的用户名为sa,密码为sa
Form1.cs的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication4
{

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void 数据增删改查_Load(object sender, EventArgs e)
{
//this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
//this.textBox1.Text = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=.";
this.textBox1.Text = "Server=.;pwd=sa;uid=sa;database=test";

}

private void button5_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(this.textBox1.Text);
con.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select * from test", con);
DataTable dt = new DataTable();
sqlda.Fill(dt);

this.dataGridView1.DataSource = dt;

}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//if (MessageBox.Show("你是否要更新数据?", "x", MessageBoxButtons.OKCancel) == DialogResult.OK)
//{
// MessageBox.Show("OK");
// SqlConnection con = new SqlConnection(this.textBox1.Text);
// con.Open();
// SqlCommand com = new SqlCommand("update test set address = , con);
// com.ExecuteNonQuery();
// //SqlDataReader dr = com.ExecuteReader();
//}
//else
//{
// MessageBox.Show("NO");
//}
}

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{

//MessageBox.Show("xxx");
}

private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
//MessageBox.Show("xxx");
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.SelectedRows.Count != 0)
{
this.textBox7.Text = this.dataGridView1.SelectedRows[0].Cells["id"].Value.ToString();
this.textBox8.Text = this.dataGridView1.SelectedRows[0].Cells["name"].Value.ToString();
this.textBox9.Text = this.dataGridView1.SelectedRows[0].Cells["address"].Value.ToString();
}
}

private void button3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你是否要更新数据?", "x", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
SqlConnection con = new SqlConnection(this.textBox1.Text);
con.Open();
SqlCommand com = new SqlCommand("update test set name= '" + this.textBox8.Text + "' ,address= '" + this.textBox9.Text+"' where id="+this.textBox7.Text,con);
com.ExecuteNonQuery();

MessageBox.Show("更新成功!");
button5_Click(null, null);
}
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你是否要删除数据?", "x", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
SqlConnection con = new SqlConnection(this.textBox1.Text);
con.Open();
SqlCommand com = new SqlCommand("delete from test where id='"+textBox7.Text+"'",con);
com.ExecuteNonQuery();

MessageBox.Show("删除成功!");
button5_Click(null, null);
}

}

private void button1_Click(object sender, EventArgs e)
{
if (this.textBox3.Text != null && this.textBox4.Text != null && this.textBox5.Text != null)
{
SqlConnection con = new SqlConnection(this.textBox1.Text);
con.Open();
SqlCommand com = new SqlCommand("insert test (id,name,address) values('"+this.textBox3.Text+"','"+this.textBox4.Text+"','"+this.textBox5.Text+"')",con);
com.ExecuteNonQuery();

MessageBox.Show("添加成功!");
button5_Click(null, null);
}
else
{
MessageBox.Show("请完整填写信息!");
}
}

private void button4_Click(object sender, EventArgs e)
{
if (this.radioButton1.Checked || this.radioButton2.Checked)
{
if (this.textBox2.Text != null)
{
if (this.radioButton1.Checked)
{
SqlConnection con = new SqlConnection(this.textBox1.Text);
con.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select * from test where id='"+this.textBox2.Text+"'", con);
DataTable dt = new DataTable();
sqlda.Fill(dt);

this.dataGridView1.DataSource = dt;
}
else
{
SqlConnection con = new SqlConnection(this.textBox1.Text);
con.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select * from test where name='" + this.textBox2.Text + "'", con);
DataTable dt = new DataTable();
sqlda.Fill(dt);

this.dataGridView1.DataSource = dt;

}
}
else
{
MessageBox.Show("请输入查询内容!");
this.textBox2.Focus();
}

}
else
{
MessageBox.Show("请选择查询方式!");
this.radioButton1.Checked = true;
}
}
}
}

Form1.Designer.cs的代码:
namespace WindowsApplication4
{
partial class Form1
{
/// <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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button5 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.id = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.name = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.address = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.textBox7 = new System.Windows.Forms.TextBox();
this.textBox8 = new System.Windows.Forms.TextBox();
this.textBox9 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(460, 301);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "添加";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(460, 359);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "删除";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(460, 272);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 3;
this.button4.Text = "查找";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(83, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(371, 21);
this.textBox1.TabIndex = 4;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 5;
this.label1.Text = "连接字符串";
//
// button5
//
this.button5.Location = new System.Drawing.Point(460, 12);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 6;
this.button5.Text = "连接数据库";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(14, 272);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(184, 21);
this.textBox2.TabIndex = 8;
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(234, 276);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(71, 16);
this.radioButton1.TabIndex = 9;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "按ID查找";
this.radioButton1.UseVisualStyleBackColor = true;
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(336, 278);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(83, 16);
this.radioButton2.TabIndex = 10;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "按Name查找";
this.radioButton2.UseVisualStyleBackColor = true;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(14, 302);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(98, 21);
this.textBox3.TabIndex = 11;
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(166, 303);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(100, 21);
this.textBox4.TabIndex = 12;
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(331, 303);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(100, 21);
this.textBox5.TabIndex = 13;
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.id,
this.name,
this.address});
this.dataGridView1.Location = new System.Drawing.Point(14, 39);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(440, 227);
this.dataGridView1.TabIndex = 18;
this.dataGridView1.UserAddedRow += new System.Windows.Forms.DataGridViewRowEventHandler(this.dataGridView1_UserAddedRow);
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
this.dataGridView1.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellEndEdit);
this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
//
// id
//
this.id.DataPropertyName = "id";
this.id.HeaderText = "id";
this.id.Name = "id";
this.id.ReadOnly = true;
//
// name
//
this.name.DataPropertyName = "name";
this.name.HeaderText = "name";
this.name.Name = "name";
this.name.ReadOnly = true;
//
// address
//
this.address.DataPropertyName = "address";
this.address.HeaderText = "address";
this.address.Name = "address";
//
// textBox7
//
this.textBox7.Location = new System.Drawing.Point(12, 342);
this.textBox7.Name = "textBox7";
this.textBox7.Size = new System.Drawing.Size(100, 21);
this.textBox7.TabIndex = 15;
//
// textBox8
//
this.textBox8.Location = new System.Drawing.Point(166, 342);
this.textBox8.Name = "textBox8";
this.textBox8.Size = new System.Drawing.Size(100, 21);
this.textBox8.TabIndex = 16;
//
// textBox9
//
this.textBox9.Location = new System.Drawing.Point(331, 342);
this.textBox9.Name = "textBox9";
this.textBox9.Size = new System.Drawing.Size(100, 21);
this.textBox9.TabIndex = 17;
//
// button3
//
this.button3.Location = new System.Drawing.Point(460, 330);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "修改";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(553, 387);
this.Controls.Add(this.dataGridView1);
this.Controls.Add(this.textBox9);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.textBox7);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button5);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "数据增删改查";
this.Load += new System.EventHandler(this.数据增删改查_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.DataGridViewTextBoxColumn id;
private System.Windows.Forms.DataGridViewTextBoxColumn name;
private System.Windows.Forms.DataGridViewTextBoxColumn address;
private System.Windows.Forms.TextBox textBox7;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.TextBox textBox9;
private System.Windows.Forms.Button button3;
}
}

热心网友 时间:2022-04-10 20:08

这个恐怕不写代码都能做到吧?你还是好好看看MICROSOFT主页关于VS。NET数据库纳部分
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
梦见好多鱼在水里活蹦乱跳 教你如何将让reaver PIN 进度随意更改精确前四位 求个保存PIN进度的方法 ...不上外接键盘,但鼠标一直有用,谁能告诉我怎么回事,先谢了。。_百度... 相机SD卡,卡上图片电脑显示不出来 相机SD卡用读卡器插到电脑上DCIM文件夹不显示照片怎么解决? win10查询错误日志的方法-win10怎么查询错误日志 电脑系统日志文件如何查看电脑里的系统日志 电脑事件日志在哪里看怎样查看电脑使用的日志 win11系统日志在哪里看 win11系统日志怎么看 西葫芦鸡蛋素馅饺子怎么做如何做好吃 C# 编程 中国最高的楼 如何看楼房风水 楼中楼装修效果 上海外滩附近的上海中心为什么呈刀鞘型? 能否设成联通的?移动的亲情号码? 移动和联通可以办亲情号码吗?? 买房子要注意哪些问题,需要注意哪几个方面比较好? 计算机三级网络技术学习经验! 请教高人!全国计算机等级考试*网络技术,有哪些推荐的教材? 考计算机三级网络技术需要哪些教材? 计算机网络技术实训教程的内容提要 计算机网络技术实用教程 计算机网络技术的学习应该从那些方面入手 如何自学计算机网络技术专业 全国计算机等级考试三级计算机网络技术要借什么书看 计算机网络技术教程的内容提要 计算机网络技术最开始开始如何自学,学的哪些(没有学过) 张家口商业银行能办信用卡嘛 张家口银行信用卡更改号码? 张家口银行信用卡超限额度 用什么软件查看打开sd卡中的文件 如何打开手机SD卡中的文件 煤气罐的主阀门旋转打开时,会听到出气声,伸鼻子过去会闻到煤气味,是漏气吗? 开煤气罐时有煤气“嘶~”泄露的声音,还带有煤气味,怎么办?减压阀 煤气罐使用的时候怎么咕噜咕噜响?而且还有一股煤气味 为什么煤气一打开就有气跑出来的声音还有一股煤气味火能点着 为什么打开煤气阀门会有嗤嗤声 煤气罐的主阀门旋转打开时,会听到出气声,伸鼻子过去会闻到煤气味,请问是漏气吗? 如何配置pix的aaa为本地认证 在交换机或者路由器上配置aaa认证后,是不是就要用认证后的帐号密码了... 神码2600的路由器配置AAA 路由配置的aaa是什么 PPP链接的AAA认证怎么配置? 领卡证明已过期,但还没通知领取社会保障卡该怎么办 有到期返保额的健康险吗 社会保障卡2年多了还没来怎么办 社保卡到期怎么办 返还险是什么? 关于返还型健康险!! “蓝牙耳机”用英语怎么说