没有合适的资源?快使用搜索试试~ 我知道了~
c# 数据库连接和操作大全
需积分: 10 8 下载量 171 浏览量
2008-10-04
20:18:30
上传
评论
收藏 7KB TXT 举报
温馨提示
试读
8页
C#连接sql server的代码和数据库的增加,删除,修改,等
资源推荐
资源详情
资源评论
一:数据库连接代码:
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("添加成功!", "啊");
}
三:数据库的修改代码:
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)
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)
剩余7页未读,继续阅读
资源评论
donglf
- 粉丝: 0
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功