有没有办法简化我的代码?

编程入门 行业动态 更新时间:2024-10-11 09:22:39
本文介绍了有没有办法简化我的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想写一个可以与Ms-Acess和MySql数据库交互的简化代码。请任何人给我们提供想法。

I want to write a simplified code which can interact with Ms-Acess and MySql database.Please anyone give us idea.

private void Form1_Load(object sender, EventArgs e) { if (dataBase == "MySql") { String conn = "server=localhost; database=sample; uid=root; password=somepassword"; using (MySqlConnection mysqlconn = new MySqlConnection(conn)) { string query = "INSERT INTO Name VALUES(" + Name + ")"; using (MySqlCommand cmd = new MySqlCommand(query, mysqlconn)) { try { mysqlconn.Open(); cmd.ExecuteNonQuery(); } catch (Exception) { } finally { mysqlconn.Close(); mysqlconn.Dispose(); } } } if (dataBase1 == "Ms Ascess") { String conn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + "sample.mdb;Jet OLEDB:Database Password=somepassword"; using (OleDbConnection oledbconn = new OleDbConnection(conn1)) { string query = "INSERT INTO Name VALUES(" + Name + ")"; using (OleDbCommand cmd = new OleDbCommand(query, oledbconn)) { try { oledbconn.Open(); cmd.ExecuteNonQuery(); } catch (Exception) { } finally { oledbconn.Close(); oledbconn.Dispose(); } } } } } }

推荐答案

首先,不要这样做 - 永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。 其次,因为你使用使用块,你不需要 finally 阻止,因为使用将在变量超出范围时处理数据。 第三,始终列出列你试图插入:它使你的代码在数据库更改时更健壮,更容易维护。 第四,永远不要吞下异常 - 这意味着你永远不会知道出了什么问题而且它会产生代码几乎不可能调试,因为你不知道它何时开始出错。记录它们,告诉用户,使应用程序崩溃。随你。但是不要吞下它们。 Firstly, don't do it like that - never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Secondly, because you are using a using block, you don't need the finally block, because the using will Dispose the data when the variable goes out of scope. Thirdly, always list the columns you are trying to insert to: it makes your code more robust and easier to maintain when the DB changes. Fourthly, never swallow exceptions - that means you never know that something has gone wrong and it makes code pretty much impossible to debug because you don't have any idea where it started to go wrong. Log 'em, tell the user, crash the app. Whatever. But don't swallow them. private void Form1_Load(object sender, EventArgs e) { if (dataBase == "MySql") { String conn = "server=localhost; database=sample; uid=root; password=somepassword"; using (MySqlConnection mysqlconn = new MySqlConnection(conn)) { string query = "INSERT INTO Name (MyColumn) VALUES(@NM)"; using (MySqlCommand cmd = new MySqlCommand(query, mysqlconn)) { mysqlconn.Open(); cmd.Parameters.AddWithValue("@NM", Name); cmd.ExecuteNonQuery(); } } } ...

使用基本接口,如 IDBConnection , IDBCommand 等: IDbCommand Interface(System.Data) [ ^ ] Use the base interfaces like IDBConnection, IDBCommand etc. : IDbCommand Interface (System.Data)[^]

更多推荐

有没有办法简化我的代码?

本文发布于:2023-06-03 23:42:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/484989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!