Access数据库多条数据Insert
由于在此之前我没有用过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); }
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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The advantage of multithreading is that it can improve performance and resource utilization, especially for processing large amounts of data or performing time-consuming operations. It allows multiple tasks to be performed simultaneously, improving efficiency. However, too many threads can lead to performance degradation, so you need to carefully select the number of threads based on the number of CPU cores and task characteristics. In addition, multi-threaded programming involves challenges such as deadlock and race conditions, which need to be solved using synchronization mechanisms, and requires solid knowledge of concurrent programming, weighing the pros and cons and using them with caution.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The main reasons why you cannot log in to MySQL as root are permission problems, configuration file errors, password inconsistent, socket file problems, or firewall interception. The solution includes: check whether the bind-address parameter in the configuration file is configured correctly. Check whether the root user permissions have been modified or deleted and reset. Verify that the password is accurate, including case and special characters. Check socket file permission settings and paths. Check that the firewall blocks connections to the MySQL server.

SQL IF statements are used to conditionally execute SQL statements, with the syntax as: IF (condition) THEN {statement} ELSE {statement} END IF;. The condition can be any valid SQL expression, and if the condition is true, execute the THEN clause; if the condition is false, execute the ELSE clause. IF statements can be nested, allowing for more complex conditional checks.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Methods to solve the cross-domain problem of Vue Axios include: Configuring the CORS header on the server side using the Axios proxy using JSONP using WebSocket using the CORS plug-in

Question: Can MySQL run on macOS? Answer: Yes. Specific instructions: It can be installed through the official MySQL installer. You can use Homebrew to install, providing command-line-driven installation methods and dependency management. Create databases and tables using the MySQL command line client. Optimize query performance and understand indexing, query cache and database standardization. Avoid conflicting version issues and use a single installation method. Ensure secure configuration, use strong passwords and access controls.
