C#WinForms

C#WinForms,第1张

花了几分钟写的,山肢历没有进行代码优化,不过你说的功能都实现了。`

请先在数据库中添加一个名字为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.1Password=saPersist Security Info=TrueUser ID=saInitial Catalog=testData Source=."

this.textBox1.Text = "Server=.pwd=sauid=sadatabase=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

}

}

Windows Forms 是微软的.NET开发框架的图形用户界面部分,该组件通过将现有的Windows API(Win32 API)封装为托管代码提碧知供了对Windows本地(native)穗带组件的访问方式.

主要用于图形用户界面的开发,虽然其它的也可以实现,但是

windows forms在进行图形用户界面的布局方猜慧芦面相对来说比其它的要快很多(比如asp.net,jsp等)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12304026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存