C#新手诚征C#实例之三(数据库访问)

Huiz 2003-11-24 12:58:54
我是一个C#新手,在Delpih编程上有点经验。诚征满足以下条件的回复,谢谢!

6、代码访问数据库
请给我一个完整的用代码访问数据库的实例。
a)、通过代码创建连接及各数据相关组件
b)、通过代码将数据填充到DataSet中,并显示在ListBox或DataGrid中
c)、通过代码将修改后的值通过DataSet更新回数据库

以上问题,其中6、100分(如果不够可以加)。
...全文
40 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangyanjun1 2003-11-24
  • 打赏
  • 举报
回复
以下示例演示如何通过显式设置 DataAdapter 的 UpdateCommand 来执行对已修改行的更新。
SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn);

catDA.UpdateCommand = new SqlCommand("UPDATE Categories SET CategoryName = @CategoryName " +
"WHERE CategoryID = @CategoryID" , nwindConn);

catDA.UpdateCommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 15, "CategoryName");

SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("@CategoryID", SqlDbType.Int);
workParm.SourceColumn = "CategoryID";
workParm.SourceVersion = DataRowVersion.Original;

DataSet catDS = new DataSet();
catDA.Fill(catDS, "Categories");

DataRow cRow = catDS.Tables["Categories"].Rows[0];
cRow["CategoryName"] = "New Category";

catDA.Update(catDS);
Huiz 2003-11-24
  • 打赏
  • 举报
回复
感谢snof,你的回答正是我需要的!
维她奶 2003-11-24
  • 打赏
  • 举报
回复
前面几位说得够详细的了。
雪狼1234567 2003-11-24
  • 打赏
  • 举报
回复
数据更新的代码也可以简化如下:不过推荐自己写sql语句:
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student", conn);
System.Data.SqlClient.SqlCommandBuilder MyCB = new System.Data.SqlClient.SqlCommandBuilder(da);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Update(dt, "HR_EMPIMAGE");
雪狼1234567 2003-11-24
  • 打赏
  • 举报
回复
首先取得数据,放到DataGrid里

System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"student");

然后绑定数据集和DataGrid
DataGrid.SetDataBinding(dt,"student");
如果需要,可以绑定TextBox来作录入,而用DataGrid显示
this.textBox16.DataBindings.Add("Text",dt,"student.stuno");
然后进行数据的操作如:
增加:
this.BindingContext[dt,"student"].AddNew();
删除:
this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);
最后把结果写回数据库:


// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO student(stuno, name) VALUES (@stuno, @name)";
this.sqlInsertCommand1.Connection = this.conn;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
this.sqlUpdateCommand1.Connection = this.conn;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50, "name"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
this.sqlDeleteCommand1.Connection = this.conn;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "stuno", System.Data.DataRowVersion.Original, null));

this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDa.InsertCommand = this.sqlInsertCommand1;
this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
try
{
sqlDa.Update(dt.GetChanges,"student");
return true;
}
catch(System.Data.SqlClient.SqlException ex)
{

return false;
}
finally
{
conn.Close();
}
changezhong 2003-11-24
  • 打赏
  • 举报
回复
1.返回一个字符串

public string getValue(string SqlCommandText)

{

string returnValue;



SqlCommand myCommand=new SqlCommand (SqlCommandText,_DataBaseConnection);



try

{

//如果数据库连接没有打开,先打开数据库连接

this.open ();

object _value=new object();

_value=myCommand.ExecuteScalar ();//得到查询结果集中的第一行第一列

_DataBaseConnection.Close();

// _DataBaseConnection=null;

returnValue=_value==null?"":_value.ToString();

//returnValue=this.getDataSet(SqlCommandText).Tables[0].Rows[0][0].ToString();

return returnValue;

}

catch(Exception err)

{

throw err;

}

}

}



2.返回一个DataSet

public DataSet getDataSet(string SqlCommandText)

{

try

{

// this.open();

SqlDataAdapter myCommand=new SqlDataAdapter (SqlCommandText,_DataBaseConnection);

DataSet ds=new DataSet();

myCommand.Fill(ds,"Table0");

// if(this._DBType==DBType.History)

// {

_DataBaseConnection.Close();

// // _DataBaseConnection=null;

// }

return ds;

}

catch(Exception err)

{

throw err;

}



} [C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的语言筛选器按钮 。


返回后与datagrid绑定

DataView dv=getDataSet(sqlstr).Tables[0].DefaultView;
datagrid1.DataSource=dv;
datagrid1.DataBind();

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧