C#连接数据库
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 连接数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//数据库连接字符串
string connStr = "server=.;uid=sa;pwd=123;database=Student" ;
//SqlConnection连接数据库
SqlConnection con=new SqlConnection(connStr);
//打开数据库
con.Open();
//数据库操作
string sql = " insert into dbo.Men values('lidonghan',23) ";
//向数据库发送命令
SqlCommand com=new SqlCommand(sql,con);
com.ExecuteNonQuery();
//关闭数据库
con.Close();
}
}
}
-------------------------------------连接数据库-------------------------------
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 数据库连接2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//连接数据库字符串
string conStr = "server=.;uid=sa;pwd=123;database=Student";
//连接数据库
SqlConnection con = new SqlConnection(conStr);
//打开数据库
con.Open();
//数据库操作
string sql = string.Format("insert into Men values('{0}',{1})", this .textBox1.Text,
this.textBox2.Text);
//向数据库发送命令
SqlCommand com = new SqlCommand(sql, con);
//执行命令
com.ExecuteNonQuery();
MessageBox.Show("数据添加成功.");
//关闭数据库
con.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Threading;
namespace 增改查数据库数据
{
public class SqlHelper
{
private static string conStr = "Data Source=.;Initial Catalog=Student;Integrated
Security=True";
//增改删
public static int ExecuteNonQuery(string sql)
{
int rowCount = -1;
using (SqlConnection conn=new SqlConnection(conStr))
{
using (SqlCommand com = new SqlCommand(sql, conn))
{
try
{
conn.Open();
rowCount=com.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
return rowCount;
}
}
}
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 增改查数据库数据
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//增
private void button1_Click(object sender, EventArgs e)
{
string sql = string.Format("insert into Men values('{0}',{1})", this.textBox1.Text.Trim(),
this.textBox2.Text.Trim());
SqlHelper.ExecuteNonQuery(sql);
}
//改
private void button2_Click(object sender, EventArgs e)
{
string sql = string.Format("update Men set name='{0}' where name='yuanlulu' ",
this.textBox1.Text.Trim());
SqlHelper.ExecuteNonQuery(sql);
}
//删
private void button3_Click(object sender, EventArgs e)
{
string sql = string.Format("delete from Men where name='{0}'",
this.textBox1.Text.Trim());
SqlHelper.ExecuteNonQuery(sql);
}
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.DodgerBlue;
}
}
}
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 存储过程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//插入、更新
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(this.label3.Text))
{
this.label3.Text = Guid.NewGuid().ToString();
}
string conStr = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
//存储过程
string sql = "sp_InsertUpdate";
SqlParameter par1 = new SqlParameter("@name", this .textBox1.Text.Trim());
SqlParameter par2 = new SqlParameter("@age", this.textBox2.Text.Trim());
using (SqlConnection conn = new SqlConnection(conStr))
{
using (SqlCommand com = new SqlCommand(sql, conn))
{
com.Parameters.Add(par1);
com.Parameters.Add(par2);
try
{
com.CommandType = CommandType.StoredProcedure;
conn.Open();
com.ExecuteNonQuery();
}
catch (Exception exception)
{
throw exception;
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.ForestGreen;
}
}
}
本文档为【C#连接数据库程序-存储过程】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。