没有合适的资源?快使用搜索试试~ 我知道了~
C#__数据库连接和操作大全+经典SQL语句大全
4星 · 超过85%的资源 需积分: 9 28 下载量 145 浏览量
2011-04-21
14:03:54
上传
评论 1
收藏 333KB PDF 举报
温馨提示
试读
29页
C#__数据库连接和操作大全+经典SQL语句大全+SQL语句学习手册实例(个人整理)
资源推荐
资源详情
资源评论
1
c# 数据库连接和操作大全.txt wxh zt
一:数据库连接代码:
SqlConnection objSqlConnection = new SqlConnection ("server = 127.0.0.1;uid = sa;
pwd =;database =test");
objSqlConnection.Open();
二:数据库的添加记录代码:
int i = 0;
string s1 = "", s2 = "";
i = Convert.ToInt16(textBox1.Text);
s1 = textBox2.Text;
s2 = textBox3.Text;
SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.
1;uid = sa; pwd =;database =test");
objSqlConnection.Open();
MessageBox.Show("数据库连接成功", "好");
try
{
SqlCommand sqlcom = new SqlCommand("insert into info(id,name,sex)
values( " + i + ",'" + s1 + "','" + s2 + "')", objSqlConnection);
sqlcom.ExecuteNonQuery();
MessageBox.Show("添加成功!", "啊");
}
catch (Exception a)
{
MessageBox.Show(a.ToString());
}
MessageBox.Show("添加成功!", "啊");
2
}
三:数据库的修改代码:
int i = 0;
string s1 = "", s2 = "";
s1 = textBox2.Text;
s2 = textBox3.Text;
if (textBox1.Text.Length == 0)
i = 0;
else
i = Convert.ToInt32(textBox1.Text);
SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.
1;uid = sa; pwd =;database =test");
objSqlConnection.Open();
MessageBox.Show("数据库连接成功", "好");
try
{
SqlCommand sqlcom = new SqlCommand("update info set name='"+s1+"',
sex='"+s2+"'"+"where id=" + i, objSqlConnection);
sqlcom.ExecuteNonQuery();
MessageBox.Show("修改成功!", "啊");
objSqlConnection.Close();
}
catch (Exception a)
{
MessageBox.Show(a.ToString());
}
四:数据库的删除代码:
int i = 0;
string s1 = "", s2 = "";
s1 = textBox2.Text;
s2 = textBox3.Text;
if(textBox1.Text.Length==0)
i=0;
else
i = Convert.ToInt16(textBox1.Text);
3
SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.
1;uid = sa; pwd =;database =test");
objSqlConnection.Open();
MessageBox.Show("数据库连接成功", "好");
try
{
SqlCommand sqlcom = new SqlCommand("delete from info where id="+i,
objSqlConnection);
sqlcom.ExecuteNonQuery();
MessageBox.Show("删除成功!", "啊");
objSqlConnection.Close();
}
catch (Exception a)
{
MessageBox.Show(a.ToString());
}
五:数据库的查询代码:
1.繻开始:
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter();
2.按钮代码:
int i = 0,n=0;
string s1 = "", s2 = "";
s1 = textBox2.Text;
s2 = textBox3.Text;
if (textBox1.Text.Length == 0)
i = 0;
else
i = Convert.ToInt32(textBox1.Text);
SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.
1;uid = sa; pwd =;database =test");
objSqlConnection.Open();
MessageBox.Show("数据库连接成功", "好");
4
string query = "SELECT * from info where id="+i;
DataSet objDataSet = new DataSet();
SqlDataAdapter obj = new SqlDataAdapter();
obj.SelectCommand = new SqlCommand(query, objSqlConnection);
obj.Fill(objDataSet, "info");
SqlCommand objSqlCommand = new SqlCommand(query,
objSqlConnection);
SqlDataReader objSqlReader = objSqlCommand.ExecuteReader();
while (objSqlReader.Read())
{
n += 1;
MessageBox.Show("编号: " + objSqlReader.Getvalue(0) + " 姓名:" +
objSqlReader.Getvalue(1) + " 性别" + objSqlReader.Getvalue(2));
}
if (n == 0)
MessageBox.Show("数据库中溡有这样的记录!");
六:数据库的查询代码:
int i = 0;
// int n = 0;
string s1 = "", s2 = "";
string sql;
s1 = textBox2.Text;
s2 = textBox3.Text;
if (textBox1.Text.Length == 0)
{
i = 0;
5
}
else
i = Convert.ToInt32(textBox1.Text);
SqlConnection objSqlConnection = new SqlConnection("server = 127.0.0.1;uid
= sa; pwd =;database =test");
objSqlConnection.Open();
MessageBox.Show("数据库连接成功", "好");
string query = "SELECT * from info where id="+i;
if(i==0)
sql = "select * from info ";
else
sql = "select * from info where id=" + i;
da1 = new SqlDataAdapter(sql, objSqlConnection);
dt1.Clear();
da1.Fill(dt1);
dataGridView1.DataSource = dt1;
数据库的币装繻代码:
class DBClass
{
public void dbclass(string sql)
{
try
{
SqlConnection sqlcon = new SqlConnection("server = 127.0.0.1;uid = sa;
pwd =;database =test");
sqlcon.Open();
SqlTransaction objt = sqlcon.BeginTransaction(); //事物开始
剩余28页未读,继续阅读
资源评论
- Nathan_nathan2013-01-24值得借鉴的东西还是挺多的
liweifeng1988
- 粉丝: 9
- 资源: 40
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功