由于在此之前我没有用过Access数据库的,当需要想数据库中插入多条数据时,我们不妨先按照sql server的做法:insert into tablename(column1,column2) values (a,b),(c,d),(e,f)。于是按照这个思路,我的第一个方案出来了。 尝试一: The Demo: StringBuild
由于在此之前我没有用过Access数据库的,当需要想数据库中插入多条数据时,,我们不妨先按照sql server的做法:“insert into tablename(column1,column2) values (a,b),(c,d),(e,f)”。于是按照这个思路,我的第一个方案出来了。
尝试一:
The Demo:
StringBuilder BuiList = new StringBuilder(string.Format("({0},0)", UserID)); foreach (RepeaterItem item in Rpt_AdminRole.Items) { if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) { HtmlInputCheckBox cbRole = item.FindControl("cb_Role") as HtmlInputCheckBox; if (cbRole.Checked) { BuiList.Append(",("); BuiList.Append(UserID); BuiList.Append(","); BuiList.Append(cbRole.Value); BuiList.Append(")"); } } }The Dal:
///exec下就会出现这样的错误:SQL 语句的结束位置缺少分号 (;)。
Access对sql的支持果然是大大精简,到此尝试一失败!, 很快在我有另外idea。sql server 多表查询对select table 的支持!我可以直接传一个DataTable到sql语句中,说干就干!
尝试二
The Demo : 获取DataTable
public DataTable GetInsertSQL(Repeater rep,string controlID) { DataTable data = new DataTable(); data.Columns.Add("AdminID"); data.Columns.Add("RoleID"); foreach (RepeaterItem item in rep.Items) { if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) { HtmlInputCheckBox cb = item.FindControl(controlID) as HtmlInputCheckBox; if (cb.Checked) { DataRow row = data.NewRow(); row.ItemArray = new object[] { UserID, cb.Value}; data.Rows.Add(row); } } } return data; }The Dal:
public static int InsertRoleContact(DataTable dt) { string sql = "insert into Sky_Admin_Role(AdminID,RoleID) select * from @Data"; OleDbParameter[] param = new OleDbParameter[] { new OleDbParameter("@Data",?){Value =dt} }; return Common.OleDbHelper.ExecuteNonQuery(CommandType.Text, sql, param); }当代码到这里我就知道此方法行不通,因为OledbType中没有对应的table类型,如果是SQL server由于支持xml可以设置为 SqlDbType.Xml类型来传递DataTable数据,由于sql本事对xml的支持 ,可以用sql基于xml的查询,本文主要讨论Access,此处暂不讨论了!尝试二宣布失败!。接下来我又想到了零时表,Access是不是也支持零时表的查询呢?
尝试三
The Demo: 取出我想要的数据格式 (1,2,3)
public string GetInserCollection(Repeater rep, string controlID) { StringBuilder buiCollecton = new StringBuilder(""); buiCollecton.Append("(0"); foreach (RepeaterItem item in rep.Items) { if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) { HtmlInputCheckBox cb = item.FindControl(controlID) as HtmlInputCheckBox; if (cb.Checked) { buiCollecton.Append(string.Format(",{0}", cb.Value)); } } } buiCollecton.Append(")"); return buiCollecton.ToString(); }
The Dal :
public static int InsertRoleContact(int UserID,string RoleCollection)
{
StringBuilder BuiSQL = new StringBuilder("");
BuiSQL.Append("declare @SkyContact table(userID int,roleID int);");
BuiSQL.Append("insert into @SkyContact values select " + UserID + ",R_ID from Sky_Role;");
BuiSQL.Append(string.Format("insert into Sky_Admin_Role values (select * from @SkyContact where roleID in {0})",RoleCollection));
return Common.OleDbHelper.ExecuteNonQuery(CommandType.Text, BuiSQL.ToString(), null);
}
这里模仿SQL Server中定义一个零时表,然后向其中插入尽可能全的数据,然后在基于零时表查询出想要的数据放入到我想要的数据中执行!exec下结果又出问错了!此处抛出这样的错误:无效的 SQL语句;期待 'DELETE'、'INSERT'、'PROCEDURE'、'SELECT'、或 'UPDATE'。其实会出错完全可以想想的到,毕竟Access中连insert into table values (1,2),(1,3) 这样的语句都不支持。此时尝试三也不得不宣告失败!尝试了这么多,我不得不使用早就准备用的方法 多条insert一起执行。
尝试四
The Demo: 先获取我想要的数据形式 :1,2,3 此处略。看sql:
public static int InsertRoleContact2(int UserID, string RoleCollection) { string[] arr = RoleCollection.Split(','); StringBuilder BuilSQL = new StringBuilder(""); foreach (string item in arr) { BuilSQL.Append( string.Format("insert into Sky_Admin_Role(AdminID,RoleID) values ({0},{1});",UserID,Convert.ToInt32(item))); } return Common.OleDbHelper.ExecuteNonQuery(CommandType.Text, BuilSQL.ToString(), null); }
ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor