请问如何在vc2005中用代码实现SQl Server数据库连接,数据查询、添加、删除等操作?急!!!

hhuifen1721 2009-08-07 04:23:38
各位高手,请问如何在vc2005中用代码实现SQl Server数据库连接,数据查询、添加、删除等操作?
...全文
227 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
deng_zhi_ping 2009-08-07
  • 打赏
  • 举报
回复
public DataTable select(string sql)
{
string Sql_Connstring = "Data Source=PC-200812201257\SQL2005;Initial Catalog=SBO_PL;User ID=sa;pwd=sasa";
SqlConnection con=new SqlConnection (Sql_Connstring);
//SqlCommand com=new SqlCommand (sql,con);
SqlDataAdapter datApte=new SqlDataAdapter (sql,con);

DataTable dt=new DataTable ();
datApte.Fill(dt);
return dt;

}


这是查询的方法 ok!
deng_zhi_ping 2009-08-07
  • 打赏
  • 举报
回复
public bool Insert(object bean,string tableName)
{
SqlConnection con = null;
try
{
con = new SqlConnection("server=.\\sqlexpress;database=wj;uid=sa;pwd=821123");
con.Open();

string sql = "Insert into " + tableName + "(";

Type t = bean.GetType();

System.Reflection.PropertyInfo[] pt = t.GetProperties();
foreach (System.Reflection.PropertyInfo p in pt)
{
sql += p.Name + ",";
}
sql = sql.Substring(0, sql.Length - 1) + ")values(";

foreach (System.Reflection.PropertyInfo p in pt)
{
sql += "@"+p.Name + ",";
}
sql = sql.Substring(0, sql.Length - 1) + ")";

SqlCommand com = new SqlCommand(sql, con);
foreach (System.Reflection.PropertyInfo p in pt)
{
SqlParameter sp = new SqlParameter("@" + p.Name,p.PropertyType);
sp.Value = p.GetValue(bean, null);
com.Parameters.Add(sp);
}

return Convert.ToBoolean(com.ExecuteNonQuery());

}
catch (Exception ex)
{
Console.Write(ex.Message);
return false;
}
finally
{
if (con != null) con.Close();
}
}

这是通用的添加数据方法
风骑士之怒 2009-08-07
  • 打赏
  • 举报
回复
sqlconnection sqlcommand

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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