本文目录一览

1,net中读写数据库需要用到那些类他们的作用

DataSet:数据存储器。DataCommand:执行语句命令。DataAdapter:数据的集合,用语填充。
sqlconnection用来创建数据库连接 sqlcommand用其中的方法来执行sql语句获取你需要的返回值

net中读写数据库需要用到那些类他们的作用

2,net中读写数据库需要用到哪些类他们的作用

Connection类Command类DataReader类DataSet类DataAdapter类DataView类等
dataset:数据存储器。datacommand:执行语句命令。dataadapter:数据的集合,用语填充。

net中读写数据库需要用到哪些类他们的作用

3,net数据库实体类

db.VoteItem.InsertOnSubmit(myvote) 这句话将数据拆入数据库中。一条数据就是一个item,一个表就是item的集合体就是集合items,那么要想将数据加入表格,就只能将item追加到表格中(也就是item的集合体items中)。也就是说item是代表表格的一条记录。而items是整个表格。
项目-右键-添加新项-选择“ado.net实体框架数据模型(根据vs版本名称有细微区别)”,然后根据向导选择数据库映射,就自动生成了对应数据库的实体类。此技术被称为linq to entity framework(又叫linq 2 ef),database first,网上查查。

net数据库实体类

4,常见网站用什么类型的数据库

随着技术和服务器硬件的升级,现在的网站居多以MSSQL或者MYSQL作为网站的数据库,ACCESS 的数据库网站已经比较少了。IDC商的虚机也会根据数据库和开发程序的不同分出一些类型,只有了解这些知识,才能找到合适的空间或服务器。1、Access一般用在小网站上,类似企业站,功能比较简单,对数据要求不高;2、Mssql是一个比较大的完善的数据库,在windows上常用,配NET ASP等程序。3、Mysql是一个小型的公开源代码的免费数据库,在windows,linux上都常用,和PHP程序组成一对完美搭档。
1、access一般用在小网站上,类似企业站,功能比较简单,对数据要求不高;2、mssql是一个比较大的完善的数据库,在windows上常用,配net asp等程序。3、mysql是一个小型的公开源代码的免费数据库,在windows,linux上都常用,和php程序组成一对完美搭档。

5,NET里怎么写数据库连接的类库

using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration;namespace GameCard.DBUtility public static class DBHelperSqlServer private static SqlConnection connection; /// /// Connection 连接 /// public static SqlConnection Connection get //读取 配置 文件 string connectionString = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; //没有这个链接 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; } } /// /// (删,改) /// /// SQL语句 /// 受影响的行数 public static int ExecuteCommand(string safeSql) SqlCommand cmd = new SqlCommand(safeSql, Connection); return cmd.ExecuteNonQuery(); } /// /// (删,改) /// /// SQL语句 /// SqlParameter[] /// 受影响的行数 public static int ExecuteCommand(string sql, params SqlParameter[] values) SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteNonQuery(); } /// /// (增) /// /// SQL语句 /// 当前行,第一列值 public static object GetScalar(string safeSql) SqlCommand cmd = new SqlCommand(safeSql, Connection); return cmd.ExecuteScalar(); } /// /// (增) /// /// SQL语句 /// SqlParameter[] /// 当前行,第一列值 public static object GetScalar(string sql, params SqlParameter[] values) SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); return cmd.ExecuteScalar(); } /// /// DataReader (查) /// /// SQL语句 /// SqlDataReader public static SqlDataReader GetReader(string safeSql) SqlCommand cmd = new SqlCommand(safeSql, Connection); SqlDataReader reader = cmd.ExecuteReader(); return reader; } /// /// DataReader (查) /// /// SQL语句 /// SqlParameter[] /// SqlDataReader public static SqlDataReader GetReader(string sql, params SqlParameter[] values) SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataReader reader = cmd.ExecuteReader(); return reader; } /// /// DataTable (查) /// /// SQL语句 /// DataTable 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]; } /// /// DataTable (查) /// /// SQL语句 /// SqlParameter[] /// DataTable public static DataTable GetDataSet(string sql, params SqlParameter[] values) DataSet ds = new DataSet(); SqlCommand cmd = new SqlCommand(sql, Connection); cmd.Parameters.AddRange(values); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); return ds.Tables[0]; } /// /// 输入 /// /// 参数 /// 类型 /// 大小 /// 值 /// SqlParameter public static SqlParameter MakeInParameter(string paraName, SqlDbType type, int size, object values) return MakeParam(paraName, (SqlDbType)type, size, ParameterDirection.Input, values); } /// /// 输出 /// /// 参数 /// 类型 /// 大小 /// 值 /// SqlParameter public static SqlParameter MakeOutParameter(string paraName, SqlDbType type, int size) return MakeParam(paraName, (SqlDbType)type, size, ParameterDirection.Output, null); } private static SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value) SqlParameter param; if (Size > 0) param = new SqlParameter(ParamName, DbType, Size); } else param = new SqlParameter(ParamName, DbType); } param.Direction = Direction; if (!(Direction == ParameterDirection.Output && Value == null)) param.Value = Value; } return param; } }}
public class sqlhelper private static string str = system.configuration.configurationmanager.connectionstrings["connstr1"].connectionstring; private static sqlconnection getconn() sqlconnection conn = new sqlconnection(str); conn.open(); return conn; } }
/// /// The SqlHelper class is intended to encapsulate high performance, /// scalable best practices for common uses of SqlClient. /// public abstract class SqlHelper //Database connection strings public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.AppSettings["SQLConnString1"]; public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.AppSettings["SQLConnString2"]; public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.AppSettings["SQLConnString3"]; public static readonly string ConnectionStringProfile = ConfigurationManager.AppSettings["SQLProfileConnString"]; // Hashtable to store cached parameters private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// /// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string /// using the provided parameters. /// /// /// e.g.: /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// /// a valid connection string for a SqlConnection /// the CommandType (stored procedure, text, etc.) /// the stored procedure name or T-SQL command /// an array of SqlParamters used to execute the command /// an int representing the number of rows affected by the command public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) SqlCommand cmd = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connectionString)) PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } } /// /// Execute a SqlCommand (that returns no resultset) against an existing database connection /// using the provided parameters. /// /// /// e.g.: /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// /// an existing database connection /// the CommandType (stored procedure, text, etc.) /// the stored procedure name or T-SQL command /// an array of SqlParamters used to execute the command /// an int representing the number of rows affected by the command public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// /// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction /// using the provided parameters. /// /// /// e.g.: /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// /// an existing sql transaction /// the CommandType (stored procedure, text, etc.) /// the stored procedure name or T-SQL command /// an array of SqlParamters used to execute the command /// an int representing the number of rows affected by the command public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return val; } /// /// Execute a SqlCommand that returns a resultset against the database specified in the connection string /// using the provided parameters. /// /// /// e.g.: /// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// /// a valid connection string for a SqlConnection /// the CommandType (stored procedure, text, etc.) /// the stored procedure name or T-SQL command /// an array of SqlParamters used to execute the command /// A SqlDataReader containing the results public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) SqlCommand cmd = new SqlCommand(); SqlConnection conn = new SqlConnection(connectionString); // we use a try/catch here because if the method throws an exception we want to // close the connection throw code, because no datareader will exist, hence the // commandBehaviour.CloseConnection will not work try PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return rdr; } catch conn.Close(); throw; } } /// /// Execute a SqlCommand that returns the first column of the first record against the database specified in the connection string /// using the provided parameters. /// /// /// e.g.: /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// /// a valid connection string for a SqlConnection /// the CommandType (stored procedure, text, etc.) /// the stored procedure name or T-SQL command /// an array of SqlParamters used to execute the command /// An object that should be converted to the expected type using Convert.To public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) SqlCommand cmd = new SqlCommand(); using (SqlConnection connection = new SqlConnection(connectionString)) PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } } /// /// Execute a SqlCommand that returns the first column of the first record against an existing database connection /// using the provided parameters. /// /// /// e.g.: /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24)); /// /// an existing database connection /// the CommandType (stored procedure, text, etc.) /// the stored procedure name or T-SQL command /// an array of SqlParamters used to execute the command /// An object that should be converted to the expected type using Convert.To public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) SqlCommand cmd = new SqlCommand(); PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); object val = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return val; } /// /// add parameter array to the cache /// /// Key to the parameter cache /// an array of SqlParamters to be cached public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters) parmCache[cacheKey] = commandParameters; } /// /// Retrieve cached parameters /// /// key used to lookup parameters /// Cached SqlParamters array public static SqlParameter[] GetCachedParameters(string cacheKey) SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey]; if (cachedParms == null) return null; SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length]; for (int i = 0, j = cachedParms.Length; i < j; i++) clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone(); return clonedParms; } /// /// Prepare a command for execution /// /// SqlCommand object /// SqlConnection object /// SqlTransaction object /// Cmd type e.g. stored procedure or text /// Command text, e.g. Select * from Products /// SqlParameters to use in the command private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms != null) foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); } }}
你要什么样的?简单的数据库连接字符串很简单:(DBHelper类)public static string connString = "server=.; uid=sa;pwd=1234; database=MySchool;";然后在类里调用SqlConnection conn = new SqlConnection(DBHelper.connString)另外一种是吧连接字符串放到配置文件.方便程序扩展public static string connString = ConfigurationManager.ConnectionStrings["MyschoolProConnString"].ToString();配置文件:

文章TAG:net  数据  数据库  哪些  net  net中读写数据库需要用到那些类他们的作用  
下一篇