表:员工
xtabs(~ df$Variable + df$Country)
tab <- xtabs(~ df$Variable + df$Country)
barplot(tab, las=1, yaxp=c(0, 100, 5), ylim=c(0, 100), ylab='Variable', col=c("red","darkblue"))
现在我在做,
code|Name|Left
----+----+----
1 | A | Y
2 | B | N
3 | C | N
4 | D | Y
5 | E | N
6 | F | Y
此代码仅获取1个数据,如何获取Left ='Y'
的所有数据答案 0 :(得分:0)
我看到的唯一错误是你迭代dr然后你试图从dr1获取数据?
试试这段代码:
SqlConnection cn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
cn.ConnectionString = "ConnectionString";
List<string> str = new List<string>();
cmd.Connection = cn;
cmd.Connection.Open();
cmd.CommandText = "SELECT code FROM employee WHERE Left = 'Y'";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
str.Add(dr.GetValue(0).ToString());
}
foreach (string p in str)
{
Console.WriteLine(p);
}
答案 1 :(得分:0)
这些是我创立的错误。
executeReader()
应为ExecuteReader()
dr1.GetValue(0)
应为dr.GetValue(0)
"<CONNECTION STRING>"
应为"<CONNECTION STRING>";
(&#39; ;
&#39;正在丢失)
如果您修复了这些错误(我假设这些错误是错误的吗?),那么您的代码应该可以正常工作。我检查了你的代码并且提取了数据。
请仔细检查您的连接字符串(因为它提取数据,我们可以假设它正常工作。但是,请检查您是否已连接到正确的数据库)&amp;查询(查询看起来也很好。但是,再次检查表和数据。在SSMS上运行它)。
SqlConnection cn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
cn.ConnectionString = @"Data Source=localhost; Initial Catalog=Testing;
Integrated Security = True"; // Changed
List<string> str = new List<string>();
cmd.Connection = cn;
cmd.Connection.Open();
cmd.CommandText = "SELECT distinct column1 FROM testTable"; // Changed
SqlDataReader dr = cmd.ExecuteReader(); // Changed
while (dr.Read())
{
str.Add(dr.GetValue(0).ToString()); // Changed
}
foreach (string p in str)
{
Response.Write(p);
}
如果您的代码仍未按预期工作,请调试您的代码(如评论中已建议的那样)。
YouTube: Debugging C# Code in Visual Studio by Programming with Mosh
Microsoft: Navigating through Code with the Debugger
Dot Net Perls: Debugging
Csharp.Net-Tutorials.com: Introduction to debugging
Michael's Coding Spot: 7 Debugging Techniques you should know in C#.NET
答案 2 :(得分:0)
有几个基本缺陷,但由于听起来你已经成功检索了至少一个记录或专栏,我将这些视为简单的拼写错误。
考虑到这一点,仍然需要进行四项更改:
我还将在此处展示一些其他更改,以帮助展示更好的实践。
//Change #1 -- new type with space for each attribute
public class Employee
{
public string Name {get;set;}
public string code {get;set;}
public bool Left {get;set;}
}
// ...
public IEnumerable<Employee> GetEmployees(bool Left)
{
//Change #2 -- ask for other fields in the the SQL select clause
string sql = "SELECT code, Name, Left FROM employee WHERE Left= @Left";
using (var cn = new SqlConnection("<CONNECTION STRING>"))
using (var cmd = new SqlCommand(sql, cn))
{
cmd.Parameters.Add("@Left", SqlDbType.Char, 1).Value = Left?"Y":"N";
cn.Open();
using (var rdr = cmd.ExecuteReader())
{
while(rdr.Read())
{
//Change #3 -- use all fields from the query results
yield return new Employee() {
Name = rdr["Name"],
code = rdr["code"],
Left = (rdr["Left"] == "Y")?true:false
};
}
rdr.Close();
}
}
}
// ...
var employees = GetEmployees(true);
foreach (var e in employees)
{
//Change #4 -- Write all fields to the response.
Response.Write("<span>{0}</span><span>{1}</span><span>{2}</span>", e.Name, e.code, e.Left );
}
答案 3 :(得分:0)
使用LINQ获取数据。 &#34;分贝&#34;你是数据库上下文。
List<String> codes = db.Employee.Where(c => c.Left == "Y").Select(c => c.code).ToList();
答案 4 :(得分:0)
我认为Left
值并非'Y'
。也许有些行以小写或某些空格尾随...为了涵盖这些情况,你可以尝试:
WHERE UPPER(Left) LIKE '%Y%'
答案 5 :(得分:0)
我认为如果你在sql server中使用存储过程会更好...你可以为查询创建一个
USE [DBName]
GO
/****** Object: StoredProcedure [dbo].[GetCode] Script Date: 03/05/2018 10:51:19 a.m. ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetCode]
(
@Left char
)
AS
BEGIN
SET NOCOUNT ON;
SELECT Code FROM Employee WHERE Employee.Left in (@Left)
END
...
然后,您可以通过这种方式从c#中调用它:
private List<Type> GetCode(string Left)
{
List<Type> List = new List<Type>();
DbConnection();
con.Open();
DynamicParameters Parm = new DynamicParameters();
Parm.Add("@Left", Left, DbType.String, ParameterDirection.Input, 2);
//Parm.Add(nameOfParameter,valueOfParameter,typeOfParameter,directionOfParameter,sizeOfParameter);
List = SqlMapper.Query<Type>(con, "Entities.GetCode", Parm, commandType: CommandType.StoredProcedure).ToList();
con.Close();
return List;
}