求C#的WINFORM MSSQL数据库操作实例,在线等

ppsniffer 2011-10-25 03:44:06
为本人刚学数C#,现在求一个数据库C#的WINFORM数据库操作类,并含一个使用例子,谢谢.最好能简单讲解一下.
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
山东蓝鸟贵薪 2011-10-25
  • 打赏
  • 举报
回复
20分呀,太少了呀
帮你顶顶吧
jevin8011 2011-10-25
  • 打赏
  • 举报
回复
下一个微软的sqlhelper
allen0118 2011-10-25
  • 打赏
  • 举报
回复
20分???

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace WindowsFormsApplication5
{
public class DBHelper
{


//string strAppConfigFile = ConfigurationManager.AppSettings["DataBaseType"].ToString();

//Helper _helper = new Helper();
//public Helper GetHelper()
//{
// switch (strAppConfigFile)
// {
// case "System.Data.OracleClient":
// _helper.DatabaseType = Helper.DatabaseTypes.Oracle;
// break;
// case "MySql.Data.MySqlClient":
// _helper.DatabaseType = Helper.DatabaseTypes.MySql;
// break;
// case "System.Data.OleDb":
// _helper.DatabaseType = Helper.DatabaseTypes.OleDb;
// break;
// case "System.Data.SqlClient":
// default:
// _helper.DatabaseType = Helper.DatabaseTypes.Sql;
// break;
// }
// return _helper;

//}


private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = "server=MMIT017\\SQL2008;database=TestDB;uid=sa;pwd=123456";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}



public static int ExecuteCommand(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}

public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}



public static int ExecuteCommand2(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}

public static string GetScalar(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
string result = cmd.ExecuteScalar().ToString();
return result;
}

public string GetScalar(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
string result = cmd.ExecuteScalar().ToString();
return result;
}

public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}

public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}

public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}

public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}

/// <summary>
/// 返回一个数据集,从里面取出不同的表
/// </summary>
/// <param name="sql"></param>
/// <param name="values"></param>
/// <returns></returns>
public static DataSet GetDataSet3(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}


}
}


*****************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnADD_Click(object sender, EventArgs e)
{
string a = "aaa";
string b = "bbb";
string c = "ccc";
string d = "ddd";
string strSql = "insert into userinfo values('" + a + "','" + b + "','" + c + "','" + d + "',)";
int i = DBHelper.ExecuteCommand(strSql);
if (i > 0)
{
MessageBox.Show("数据增加成功");
}
else
{
MessageBox.Show("数据增加失败");
}
}

private void btnDelete_Click(object sender, EventArgs e)
{
string UserName = "aaa";
string strSql = "delete userinfo where username='"+UserName+"'";
int i = DBHelper.ExecuteCommand(strSql);
if (i > 0)
{
MessageBox.Show("用户删除成功");
}
else
{
MessageBox.Show("用户删除失败");
}

}

private void btnUpdate_Click(object sender, EventArgs e)
{
string UserName = "aaa";
string address = "北京市北京路1号";
string strSql = "update userinfo set UserAddress='"+address+"' where username='" + UserName + "'";
int i = DBHelper.ExecuteCommand(strSql);
if (i > 0)
{
MessageBox.Show("用户信息更新成功");
}
else
{
MessageBox.Show("用户信息更新失败");
}
}

private void btnSearch_Click(object sender, EventArgs e)
{
string strSql = "select * from userinfo";
DataTable dt = DBHelper.GetDataSet(strSql);
this.dataGridView1.DataSource = dt;

}
}
}
wnyxy001 2011-10-25
  • 打赏
  • 举报
回复
在百度上谷歌一下就知道了

110,571

社区成员

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

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

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