sql查询将数据保存在数据库中 [英] sql query to save data in data base
本文介绍了sql查询将数据保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想保存Albeant的Sql查询语法,对于列(日期)和数据类型(日期和时间)表名称(persdat)
Sql query syntax I want to save Albeant, for the column (date) and the type of data (date and time) the table name (persdat)
推荐答案
static MySqlConnection connection;
public void Add(Test test)
{
openConnection();
string query = "INSERT INTO Math (dateTime,degree,testTime) VALUES('" + String.Format("{0:yyyy-M-d HH:mm:ss}", test.DateTime) + "','" + test.Degree + "','" + test.TestTime + "')";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
closeConnection();
}
private void openConnection()
{
try
{
connection = new MySqlConnection("SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + username + ";" + "PASSWORD=" + password + ";");
connection.Open();
Isconnected = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Isconnected = false;
}
}
private void closeConnection()
{
try
{
connection.Close();
Isconnected = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
MySql.Data.dll[^]
这可能会有所帮助,
Sql_insert.asp [使用C#读取,插入,更新和删除简单的ADO.NET数据库. [
It might be helpful,
Sql_insert.asp[^]
Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]
:)
阅读本文, SQL注入攻击以及有关如何进行注入的提示防止它们 [尤其是防止 ^ ]参数化查询部分.它将帮助您解决问题,并使您正确地开始.
Read this article, SQL Injection Attacks and Some Tips on How to Prevent Them[^], especially the part on parameterised queries. It will help with your problem and get you started on the right track.
这篇关于sql查询将数据保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文