C# 连接数据库,查询记录问题

wujun_dry 2010-09-01 02:24:52

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using MySQLDriverCS;
using System.Data.Odbc;
using System.Windows.Forms;

namespace PhotoApplication
{
class DatatBaseDeal
{
public DatatBaseDeal() { }
public int RunMySQL(string sql)
{
MySQLConnection conn = null;
MySQLCommand commn = null;
int data = 0;
try
{
conn = new MySQLConnection(new MySQLConnectionString("localhost", "photo", "root", "", 3309).AsString);
conn.Open();
commn = new MySQLCommand(sql, conn);
data = commn.ExecuteNonQuery();
MessageBox.Show(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
return data;
}

/// <summary>
/// 查询编号
/// </summary>
/// <returns></returns>
public Boolean selectGround(string str)
{


//string sql = "select '" + str + "' from ground";
string sql = "select count(ground_number) from ground where ground_number = 'aaa-11'";
int resultDeal = RunMySQL(sql);
if (resultDeal == 1)
{
return true;
}
else
{
return false;
}
}
}
}



查询不到数据,数据库里面绝对有数据!请高手帮忙啊
...全文
197 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wujun_dry 2010-09-02
  • 打赏
  • 举报
回复
非常感谢各位,小弟以后一定多注意细节方面。谢谢各位!
laichunlin 2010-09-01
  • 打赏
  • 举报
回复
commn.ExecuteNonQuery()返回的是影响的行数,
要查询到数据库中的数据可以用
mySqlDataAdapater da=new mySqlDataAdapater (cmd);

DataTable dt=new DataTable();

da.fill(dt);

此时dt中就会有数据
pclsprest 2010-09-01
  • 打赏
  • 举报
回复
我晕...执行无返回的操作,是做添加修改的吧.返回的值是影响的行,你一个查询用这个.当然会出现你所谓的没有数据了
aspall 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yongchaochu 的回复:]
如果是控制台为什么有MessageBox.Show()?
如果是winform为什么有Console.Write()?
没搞明白楼主是做的哪种程序。。
public Boolean selectGround(string str)
{


//string sql = "select '" + str + "' from ground";
string sql = "……
[/Quote]

顶下; 说的观点都支持
b87936260 2010-09-01
  • 打赏
  • 举报
回复
呵呵,我也老糊涂
DevinHu 2010-09-01
  • 打赏
  • 举报
回复
LZ 我差点也被你给忽悠了,认真些好,小问题,你应该可以解决的!
sunhuaiwei 2010-09-01
  • 打赏
  • 举报
回复
顶一下


wuyq11 2010-09-01
  • 打赏
  • 举报
回复
ExecuteReader()方法将SQL语句发送给SqlConnection并生产一个SqlDataReader类对象,该SqlDataReader对象包含SQL命令返回的数据;
ExecuteScalar()方法执行SQl查询,并返回查询结果集中的第一行的第一列
int count= (int)cmd.ExecuteScalar();
yongchaochu 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jointan 的回复:]
改为: data = (int)commn.ExecuteScalar();
[/Quote]存储过程能在一定程度防止注入 但不是绝对
yongchaochu 2010-09-01
  • 打赏
  • 举报
回复
对了,commn.ExecuteNonQuery()他是针对非查询的Insert,update,delete用的,查询的话可以用ExecuteScalar()返回第一行记录或者ExecuteReader(),通过reader.GetString(index)获取到你要取的数据库中对应字段的值
yongchaochu 2010-09-01
  • 打赏
  • 举报
回复
如果是控制台为什么有MessageBox.Show()?
如果是winform为什么有Console.Write()?
没搞明白楼主是做的哪种程序。。
public Boolean selectGround(string str)
{


//string sql = "select '" + str + "' from ground";
string sql = "select count(ground_number) from ground where ground_number = 'aaa-11'";
int resultDeal = RunMySQL(sql);
if(resultDeal!=0)
{Console.Write(result.ToString());}
}
  • 打赏
  • 举报
回复
需要从SQL查询中返回一个值,如表中记录数。可以使用ExecuteScalar()方法,这个方法只返回一个值。
using System.Data;
using System.Data.SqlClient;

namespace ExecuteScalar
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(
@"Data Source=scott;Initial Catalog=northwind;Persist Security Info=True;User ID=sa;Password=sa123");

thisConnection.Open();

SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "select count(*) from customers";

//ExecuteScalar:执行只返回一个值的SQL命令。
object countResult = thisCommand.ExecuteScalar();
Console.WriteLine("Count of Customers={0}",countResult);

Console.ReadLine();

thisConnection.Close();
}
}
}

chazikai24 2010-09-01
  • 打赏
  • 举报
回复
data = commn.ExecuteNonQuery();

这个data返回的是受影响的行数,适用于update或者insert,查询select是没用的。
你要用commn.ExecuteScalar();
  • 打赏
  • 举报
回复
对于 Update、Insert 和 Delete 语句,返回值为该命令---commn.ExecuteNonQuery()所影响的行数。对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。
chazikai24 2010-09-01
  • 打赏
  • 举报
回复
sql语句在数据库sql查询器能得到查询结果?
zhoujk 2010-09-01
  • 打赏
  • 举报
回复
建议使用存储过程,在SQl的环境中先测试好,然后再到 C# 中加入参数进行调用就行了。
存储过程可以防止注入,并且它的过滤是在服务器上进行的,当数据量太大时,在本地进行的话,会先将服务器上的所有数据传送到本地机,然后进行过滤和显示,这样做太消耗带宽和内存。
jointan 2010-09-01
  • 打赏
  • 举报
回复
改为: data = (int)commn.ExecuteScalar();

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧