我正在尝试创建一个类来获取和设置sql c中的值# [英] I am trying to create a class to get and set values within sql c#
问题描述
大家好,
所以我试图在我的应用程序中创建一个类来启动与sql的连接,并在表中获取和设置值
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Configuration;
使用 System.Data.SqlClient;
命名空间 ConsoleApplication1
{
public class EQData
{
public string x_id { get ; set ; }
public string fullname { get 跨度>; set ; }
public string email { get 跨度>; set ; }
public EQData()
{
SQLConnect sqlcon = new 的SQLConnect();
SqlDataReader myReader = null ;
SqlCommand myCommand = new SqlCommand( select x_id,fullname,email FROM cas_user_ext WHERE fullname = @fullname,
sqlcon.Connection);
myCommand.Parameters.AddWithValue( @ fullname, Ivan Lubbe);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
x_id = myReader [ x_id];
fullname = myReader [ fullname];
email = myReader [ email];
}
sqlcon.Close();
}
}
}
这是我的代码。老实说,我有点困惑,我正在看2x教程(示例代码),他们让我困惑
http://stackoverflow.com/questions/13410210/creating-a-class-to-interact-with-a- sql-database [ ^ ]
http://msdn.microsoft.com/en-us/library/ms173109.aspx [ ^ ]
我收到错误无法隐式转换'对象'为'字符串'
我用来引用我班级的行是
var EQData = new EQData();
// EQData.fullname = myReader [Test User];
Console.WriteLine(EQData.fullname);
// string name = EQData.fullname;
我已经测试了几行,但我显然没有做到这一点。并且实际上没有合适的教程解释如何使用get和set来读取和写入数据到sql的类。任何参考和帮助将不胜感激。
您的问题是这段代码:
x_id = myReader [ x_id];
fullname = myReader [ fullname];
email = myReader [ email];
数据读取器将对象存储为对象类型,并且您尝试将它们分配给字符串。
您可以这样做:
x_id = myReader [ 。X_ID跨度>]的ToString();
fullname = myReader [ fullname]。ToString();
email = myReader [ email]。ToString();
但请注意,当您处理不同类型时,您需要进行适当的转换。
如:
如果你的ID实际上是一个int而不是一个字符串
x_id = Int32 .Parse(myReader [ x_id]);
问题应该在这里:
x_id = myReader [ x_id];
这部分代码将返回值作为对象。你不能设置变量哪个是字符串对象值。
解决方案:
x_id = myReader [ x_id]。ToString();
fullname = myReader [ fullname]。ToString();
email = myReader [ email]。ToString();
Hi all,
So im trying to create a class within my application that initiates a connection to sql and also gets and sets values within a table
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
public class EQData
{
public string x_id { get; set; }
public string fullname { get; set; }
public string email { get; set; }
public EQData()
{
SQLConnect sqlcon = new SQLConnect();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("select x_id, fullname, email FROM cas_user_ext WHERE fullname = @fullname",
sqlcon.Connection);
myCommand.Parameters.AddWithValue("@fullname", "Ivan Lubbe");
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
x_id = myReader["x_id"];
fullname = myReader["fullname"];
email = myReader["email"];
}
sqlcon.Close();
}
}
}
This is my code. Honestly i am a little confused i'm looking at 2x tutorials (sample code) and they are confusing me
http://stackoverflow.com/questions/13410210/creating-a-class-to-interact-with-a-sql-database[^]
http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]
I am getting the error "Cannot implicitly convert 'object' to 'string'
The lines i'm using to reference my class is
var EQData = new EQData();
// EQData.fullname = myReader["Test User"];
Console.WriteLine(EQData.fullname);
//string name = EQData.fullname;
I've commented out a few lines as i tested but i'm obviously not getting this right. And there are really no decent tutorials explaining on how to use a class with get and set to read and write data to sql. Any references and help would be greatly appreciated.
Your issue is this section of code:
x_id = myReader["x_id"]; fullname = myReader["fullname"]; email = myReader["email"];
The Data reader stores the objects as "object" types and you are trying to assign them to strings.
You could do:
x_id = myReader["x_id"].ToString(); fullname = myReader["fullname"].ToString(); email = myReader["email"].ToString();
But be aware when you are handling different types you need to do the appropriate conversion.
Such as:
If your ID was in fact an int instead of a string
x_id = Int32.Parse(myReader["x_id"]);
Hi,
problem should be here:
x_id = myReader["x_id"];
this part of code will return value as object. You can not set variable which is string object value.
Solution:
x_id = myReader["x_id"].ToString(); fullname = myReader["fullname"].ToString(); email = myReader["email"].ToString();
这篇关于我正在尝试创建一个类来获取和设置sql c中的值#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!