如何在c#中使用varchar数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16112982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use the varchar data type in c#?
提问by elena
I have this piece of code and I want to make it varchar not string. What is the appropriate syntax to do that in c#
我有这段代码,我想让它 varchar 不是字符串。在 c# 中执行此操作的适当语法是什么
string emri = row["Nome"].ToString();
I have a query that I have to run and I use emri in it and compare it with a column in table which I have created in mysql. the column in mysql is of type varchar(20). When I execute my code it gives an error in my query and I was guessing maybe it was for this reason
我有一个必须运行的查询,我在其中使用 emri 并将其与我在 mysql 中创建的表中的列进行比较。mysql 中的列是 varchar(20) 类型。当我执行我的代码时,它在我的查询中给出了一个错误,我猜可能是这个原因
I have this query
string query = "IF NOT EXISTS(SELECT * FROM clienti WHERE CodCliente= " + id + " AND Nome = '" + emri + "' AND RagioneSociale=' " + ragSoc + " ' AND PartitaIVA=' " + piva + " ') INSERT INTO clienti VALUES(" + id + " ,' " + emri + " ',' " + ragSoc + " ',' " + piva + " ') else UPDATE clienti SET( " + id + " ,' " + emri + " ',' " + ragSoc + " ',' " + piva + " ')";
and it gives me this problem
它给了我这个问题
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS(SELECT * FROM clienti WHERE CodCliente= 1 AND NomeCliente = 'Jo' at line 1
回答by tnw
There is no varchartype in C#.
varcharC# 中没有类型。
A varcharis pretty much a string (of variable length in SQL, obviously), so what you have is fine.
Avarchar几乎是一个字符串(在 SQL 中显然是可变长度的),所以你所拥有的很好。
回答by Lasse V. Karlsen
In .NET there is no type named "varchar", the closest thing you got is string.
在 .NET 中没有名为“varchar”的类型,你得到的最接近的是string.
I'm assuming there is a reason you're asking, and I'm guessing you're having some problems with the code you've posted in the question, so let me try to guess what that is, and tell you how to solve it.
我假设你问是有原因的,我猜你在问题中发布的代码有一些问题,所以让我试着猜猜那是什么,并告诉你如何解决这个问题。
First, since varchardoesn't exist, the code as you've posted it looks fine, on the surface.
首先,由于varchar不存在,您发布的代码在表面上看起来不错。
However, there is a problem, you don't show what rowis, but if it is one of the usual culprits, you're going to run into problems with nullmarks from the database.
但是,有一个问题,您没有显示是什么row,但是如果它是常见的罪魁祸首之一,那么您将遇到null来自数据库的标记问题。
A nullmark in the database in the corresponding column will not be returned as nullto your .NET code, instead you're going to get back a DBNull.Valuevalue.
一个null在相应列的数据库标志不会被返回null到你的.NET代码,而不是你要得到一个DBNull.Value值。
Here's what I would write instead:
这是我会写的:
string emri;
if (row["Nome"] == DBNull.Value)
emri = null; // or String.Empty if you don't like null values
else
emri = Convert.ToString(row["Nome"]);
回答by NotAnExpertBut...
NO this is untrue, When you step through a process by changing the debugging settings located in VS you can step through a SQL script process also if you run an example like this where a table has varchar values set into it.
不,这是不正确的,当您通过更改位于 VS 中的调试设置来逐步执行一个过程时,如果您运行这样的示例,其中表中设置了 varchar 值,您也可以逐步执行 SQL 脚本过程。
Try
{
using (SqlCommand cmd = new SqlCommand(examplestring, connection))
{
cmd.ExecutenonQuery();
}
}
catch
{
return;
}
now put a breakpoint onto the catch statement and in the examplestring have a string called '456789 - 1' equal to Itemnumber varchar(25) in the SQL table.
现在在 catch 语句上放置一个断点,并且在 examplestring 中有一个名为 '456789 - 1' 的字符串,等于 SQL 表中的 Itemnumber varchar(25)。
The catch will get the following error SqlException was caught - Error converting data type varchar to numeric.
catch 将得到以下错误 SqlException was catched - Error conversion data type varchar to numeric。
Now query string is set as a string cause that is the only way the SqlCommand works look it up on MSDN : System.Data.SqlClient.SqlCommand
现在查询字符串被设置为字符串原因,这是 SqlCommand 在 MSDN 上查找它的唯一方法: System.Data.SqlClient.SqlCommand
So yes Lasse there is varchar in .NET.
所以是的,Lasse 在 .NET 中有 varchar。

