各位好,用C#连接数据库,表中有两列是wav,element。按条件查询时wav能查询到,element不能。程序如下,求指教。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;namespace database2
{
class Program
{
static void Main(string[] args)
{
MySqlConnection conn; // mysql连接
MySqlDataAdapter myadp,myadp1; // mysql数据适配器
DataSet myds,myds1; // 数据集
DataTable mytable,mytable1;
string MyConnectionString;
MyConnectionString = "server = localhost; uid = root; pwd = mysql; database = spec_line";
double X = 318.115;
try
{
//打开菜单配置数据库连接
conn = new MySqlConnection(); // 实例化数据库连接(instanced)
conn.ConnectionString = MyConnectionString; // 配置连接(configured)
conn.Open(); // 打开连接(opened)
myadp = new MySqlDataAdapter("select * from spec_line where Wav >305.12, conn);
mytable = new DataTable();
myds = new DataSet();
// 填充和绑定数据
myadp.Fill(mytable);
int col = mytable.Columns.Count;
foreach (DataRow item in mytable.Rows)
{
for (int i = 0; i < col; i++)
{
Console.Write(item[i].ToString() + " ");
}
Console.WriteLine();
}
myadp1 = new MySqlDataAdapter("select * from spec_line where Element = Ca", conn);
mytable1 = new DataTable();
myds1 = new DataSet();
// 填充和绑定数据
myadp1.Fill(mytable1);
int col1 = mytable1.Columns.Count;
foreach (DataRow item1 in mytable1.Rows)
{
for (int j = 0; j < col1; j++)
{
Console.Write(item1[j].ToString() + " ");
}
Console.WriteLine();
}
Console.ReadKey();
} catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
Console.WriteLine("不能连接到数据库服务器,请联系数据库管理员!"); break;
case 1045:
Console.WriteLine("无效的用户名/密码,请重试!"); break;
case 1049:
Console.WriteLine("数据库不存在,或数据库名错误"); break;
default:
Console.WriteLine(ex.Message); break;
}
}
}
}
}
谢谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;namespace database2
{
class Program
{
static void Main(string[] args)
{
MySqlConnection conn; // mysql连接
MySqlDataAdapter myadp,myadp1; // mysql数据适配器
DataSet myds,myds1; // 数据集
DataTable mytable,mytable1;
string MyConnectionString;
MyConnectionString = "server = localhost; uid = root; pwd = mysql; database = spec_line";
double X = 318.115;
try
{
//打开菜单配置数据库连接
conn = new MySqlConnection(); // 实例化数据库连接(instanced)
conn.ConnectionString = MyConnectionString; // 配置连接(configured)
conn.Open(); // 打开连接(opened)
myadp = new MySqlDataAdapter("select * from spec_line where Wav >305.12, conn);
mytable = new DataTable();
myds = new DataSet();
// 填充和绑定数据
myadp.Fill(mytable);
int col = mytable.Columns.Count;
foreach (DataRow item in mytable.Rows)
{
for (int i = 0; i < col; i++)
{
Console.Write(item[i].ToString() + " ");
}
Console.WriteLine();
}
myadp1 = new MySqlDataAdapter("select * from spec_line where Element = Ca", conn);
mytable1 = new DataTable();
myds1 = new DataSet();
// 填充和绑定数据
myadp1.Fill(mytable1);
int col1 = mytable1.Columns.Count;
foreach (DataRow item1 in mytable1.Rows)
{
for (int j = 0; j < col1; j++)
{
Console.Write(item1[j].ToString() + " ");
}
Console.WriteLine();
}
Console.ReadKey();
} catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
Console.WriteLine("不能连接到数据库服务器,请联系数据库管理员!"); break;
case 1045:
Console.WriteLine("无效的用户名/密码,请重试!"); break;
case 1049:
Console.WriteLine("数据库不存在,或数据库名错误"); break;
default:
Console.WriteLine(ex.Message); break;
}
}
}
}
}
谢谢。
解决方案 »
- RDLC 导出到ECXEL,每行的高度都自动变小了
- C# QQ连连看外挂(内存版)源代码
- C# 数据库里的数据读取出来必须是DataTable吗?
- 未处理 System.ExecutionEngineException 异常
- 怎么隐藏menuStrip下的内容
- 无光驱,无软驱,如何从硬盘安转操作系统,并且要格式化c盘?
- 提问一个教科书上的题目...我还有50全送上 紧急/!!
- 程序怎样可以获得 ms sql server 的 data 的安装路径?
- 如何用c#向Access中添加新记录??
- 谁有关于DirectX9(managed)方面的资料,谢谢各位大侠。
- 二位数组与交错数组
- 关于获取多个屏幕顺序的问题?
myadp1 = new MySqlDataAdapter("select * from spec_line where Element = Ca", conn);
=》
myadp1 = new MySqlDataAdapter("select * from spec_line where Element = ‘Ca’", conn);
myadp1 = new MySqlDataAdapter("select * from spec_line where Element = " + mytable.Rows[0][1].ToString(), conn);
因为值不是固定的‘Ca’.是需要mytable.Rows[0][1].ToString()得到的。
myadp1 = new MySqlDataAdapter("select * from spec_line where Element = '" + mytable.Rows[0][1].ToString()+"'", conn);
//或者使用string.Format();
myadp1 = new MySqlDataAdapter(string.Format("select * from spec_line where Element = '{0}'",mytable.Rows[0][1].ToString()), conn);