SQL数据库插入问题

SQL数据库插入问题

本文介绍了SQL数据库插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据插入数据库.

这是我的代码:

Hi, I''m trying to insert data into a database.

This is my code:

SqlCeConnection conn = new SqlCeConnection(connStr);
conn.Open();

string cmd = "INSERT INTO Log(Type,Rego) VALUES(" + typeBox.Text + "," +     regoBox.Text + ")";

SqlCeCommand writeToDB = new SqlCeCommand(cmd, conn);
writeToDB.ExecuteNonQuery();
conn.Close();



Type是一个ntext列,Rego是一个浮点数.当我将DA40放入typeBox中并将3.0放入regoBox中时,出现此错误:

列名无效. [节点名称(如果有)=,列名称= DA40]

我在做傻事吗?
感谢您提供一些帮助,谢谢



Type is an ntext column and Rego is a float. When I put DA40 in typeBox and 3.0 in regoBox I come up with this error:

The column name is not valid. [Node name (if any)=,Column name=DA40]

Am I doing something silly?
Some help would be most appreciated, thanks

推荐答案

string cmd = "INSERT INTO Log(Type, Rego) VALUES('" + typeBox.Text + "'," + regoBox.Text + ")";



参数化查询版本.



Parameterized Query Version.

SqlCeConnection conn = new SqlCeConnection(connStr);
conn.Open();

string cmd = "INSERT INTO Log(Type, Rego) VALUES(@Type, @Rego)";

SqlCeCommand writeToDB = new SqlCeCommand(cmd, conn);
writeToDB.Parameters.Add(new SqlCeParameter("@Type", typeBox.Text);
writeToDB.Parameters.Add(new SqlCeParameter("@Rego", regoBox.Text);
writeToDB.ExecuteNonQuery();
conn.Close();



string cmd = "INSERT INTO Log(Type,Rego) VALUES('" + typeBox.Text + "'," + regoBox.Text + ")";



将此行放入您的代码中.



put this line into ur code.


这篇关于SQL数据库插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:57