读书人

怎么实现以下的功能

发布时间: 2012-01-14 20:02:35 作者: rapoo

如何实现以下的功能?
要写一个数据操作类,操作的表是Contac,字段:title,content,inputtime

如何实现以下5个方法,应该怎么写呢?谢谢!!!!

1、public DataSet GetAllContacts() //获取Contact表的所有记录
{
try
{
// TO DO: Implement
return new DataSet();
}
catch (Exception ex)
{
return new DataSet();
}
}

2、public DataSet SearchContacts() //搜索Contact中符合要求的纪录
{
try
{
// TO DO: Implement

return new DataSet();
}
catch (Exception ex)
{
return new DataSet();
}
}

3、public DataSet GetContactDetails(int aContactId) //获取某条详细记录。
{
try
{
// TO DO: Implement

return new DataSet();
}
catch (Exception ex)
{
return new DataSet();
}
}

4、public void UpdateContact(int aContactId) //修改记录
{
try
{
// TO DO: Implement
}
catch (Exception ex)
{
}
}



5、public void AddContact(string aFirsName, string aSurname, string aCompany,
string aJobTitle, string aEmail, DateTime aStartDate) //添加记录
{
try
{


}
catch (Exception ex)
{
string str=ex.ToString();

}

}


[解决办法]
1、public DataSet GetAllContacts() //获取Contact表的所有记录
{
try
{
SqlClient.SqlDataAdapter adpt = new SqlClient.SqlDataAdapter( "select * from Contac ",myConnection);
DataSet ds = new DataSet();
adpt.Fill(ds);
return ds;
}
catch (Exception ex)
{
return new DataSet();
}
}
2、public DataSet SearchContacts(string filter) //搜索Contact中符合要求的纪录
{
try
{
SqlClient.SqlDataAdapter adpt = new SqlClient.SqlDataAdapter( "select * from Contac where " + filter ,myConnection);


DataSet ds = new DataSet();
adpt.Fill(ds);
return ds;
}
catch (Exception ex)
{
return new DataSet();
}
}
3、public DataSet GetContactDetails(int aContactId) //获取某条详细记录。
{
try
{
SqlClient.SqlDataAdapter adpt = new SqlClient.SqlDataAdapter( "select * from Contac where ContactId= " + aContactId.ToString()) ,myConnection);
DataSet ds = new DataSet();
adpt.Fill(ds);
return ds;
}
catch (Exception ex)
{
return new DataSet();
}
}

4、public void UpdateContact(int aContactId,string atitle,string acontent,string ainputtime) //修改记录
{
try
{
SqlClient.SqlCommand cmd = new SqlClient.SqlCommand ( "update Contac set title= ' " + atitle + " ',content= ' " +acontent+ " ',inputtime= ' " + ainputtime + " ' where ContactId= " +aContactId.ToString(),MyConnection);
cmd.Conection.Open();
int re =cmd.ExecuteNonQuery();
if(re==0)
{
//失败;
}
else
{
//成功;
}
}
catch (Exception ex)
{
}
}
5、public void AddContact(string aFirsName, string aSurname, string aCompany,
string aJobTitle, string aEmail, DateTime aStartDate) //添加记录
{
try
{

SqlClient.SqlCommand cmd = new SqlClient.SqlCommand ( "insert Contac (FirsName,Surname,Company,JobTitle,Email,StartDate) Values ( ' "+ aFirsName + " ', ' " + aSurname + " ', " " + aCompany + " ', ' " + aJobTitle + " ', ' " + aEmail + " ', ' " + aStartDate.ToString() + " ') " ,MyConnection);
cmd.Conection.Open();
int re =cmd.ExecuteNonQuery();
if(re==0)
{
//失败;
}
else
{
//成功;
}

}
catch (Exception ex)
{
string str=ex.ToString();

}

}

[解决办法]
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong~

读书人网 >C#

热点推荐