日期:2014-05-18  浏览次数:20991 次

求一个C#访问数据库学习的例子...
求一个C#访问数据库学习的例子... 

数据库为SQL Server 2000
对数据的访问包括数据检索,获取数据,并增删改基本数据操作。

------解决方案--------------------
这个太简单了,随便找本书看看吧
------解决方案--------------------
try
{
SqlConnection sqlConnection1=new SqlConnection("Data Source=.;Initial Catalog=Hemis;Integrated Security=True");
sqlConnection1.Open();
SqlCommand sqlCommand1=sqlConnection1.createcommand();
string fn = "insert into tongxunlu values('" + this.name.Text + "','" + this.sex.Text + "',";
fn += "'" + this.birthday.Text + "','" + this.telephone.Text + "','" + this.address.Text + "','" + this.QQ.Text + "','" + this.Email.Text + "')";
//将新建的记录添加到数据库中
sqlCommand1.CommandText = fn;
int n = sqlCommand1.ExecuteNonQuery();
MessageBox.Show("添加记录成功!");
sqlConnection1.Close();

}
catch (Exception Err)
{
if (sqlConnection1.State == ConnectionState.Open)
{
sqlConnection1.Close();
}
MessageBox.Show(Err.Message);

}

更新的sql语名,参考: sqlCommand1.CommandText = "update tongxunlu set 性别='" + this.sex.Text + "',出生日期='" + this.birthday.Text + "',电话='" + this.telephone.Text + "',家庭住址='" + this.address.Text + "',QQ='" + this.QQ.Text + "',Email='" + this.Email.Text + "' where 姓名='" +this.name.Text + "'";

------解决方案--------------------
SqlConnection con;
con=new SqlConnection("server=.;database=servername;uid=sa;pwd=sa;");

Sqlcommand updatecommand=new Sqlcommand("update Tongxunlu set name=@name,pwd=@pwd");//增加、删除、修改就在Sqlcommand里用SQL语句
updatecommand.Connection=con;

updatecommand.Parameters.add(new sqlpatameter("@name",SqlDbType.Nchar,10));
updatecommand.Parameters.add(new sqlpatameter("@pwd",SqlDbType.Nchar,20));

updatecommand.Parameters["@name"].Values=TextBox1.Text;
updatecommand.Parameters["@pwd"].Values=TextBox2.Text;

con.open();
updateCommand.ExecuteNonQuery();
con.close();


------解决方案--------------------
http://dotnet.aspx.cc/article/8ade535f-ad40-4de3-a962-a64b4faf12c4/read.aspx
利用DataGrid编辑、修改、删除记录
------解决方案--------------------
可以上msdn学习
------解决方案--------------------
楼主的问题可以写一本书
------解决方案--------------------
http://asp.51aspnet.net/bbs/checkasp.asp?id=420

看下这里~
------解决方案--------------------
探讨
楼主的问题可以写一本书

------解决方案--------------------
先定义一个连接数据库的字符串,再添几个方法分别用来执行SQL语句或存储过程等就可以实现你的要求了.
比如: 连接数据库的字符串: protected static string connectionString = @"
server=localhost;
database=;
integrated security=sspi;";

执行SQL语句的方法:
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>