有人可以帮助我与SQL数据读取器 [英] can somebody help me with sql data reader

查看:59
本文介绍了有人可以帮助我与SQL数据读取器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关从数据库读取数据的帮助.我已经尝试过这段代码,但是它似乎没有在我想要的文本框中显示结果. where子句中的文件名"已经定义.只是这里不懂

I need help with reading data from my database. I have tried this piece of code but it doesnt seem to display the results in the text box i want. The ''filename'' in the where clause is already defined. its just not incluced here

<code><pre>

字符串连接,sql;

连接= Properties.Settings.Default.cvmanagerConnectionString;
sql =从其中FileLoc =""+文件名+""的应用程序中选择FileLoc,Fname,Lname;

试试
{
SqlDataReader reader = null;
SqlConnection conn =新的SqlConnection(connection);
SqlCommand cmd =新的SqlCommand(sql,conn);

conn.Open();
reader = cmd.ExecuteReader();

如果(reader.HasRows)
{
while(reader.Read())
{
tbappname.Text = reader ["Fname"].ToString();
}
}
}
catch(ex ex例外)
{
MessageBox.Show(ex.ToString());
}

String connection, sql;

connection = Properties.Settings.Default.cvmanagerConnectionString;
sql = "select FileLoc, Fname, Lname from apps where FileLoc = ''" + filename + "''";

try
{
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
reader = cmd.ExecuteReader();

if (reader.HasRows)
{
while (reader.Read())
{
tbappname.Text = reader["Fname"].ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

推荐答案

SqlCommand cmd = new SqlCommand(从其中FileLoc =''" + TextBox1.Text +",conn的应用程序中选择FileLoc,Fname,Lname );
conn.open();
SqlDataReader reader = cmd.ExecuteReader();
if(reader.Read())
{

tbappname.Text = reader ["Fname"].ToString();
reader.Close();
conn.Close();
}
其他
{
字符串message =未找到记录";
reader.Close();
conn.Close();
}
SqlCommand cmd = new SqlCommand("select FileLoc, Fname, Lname from apps where FileLoc = ''" + TextBox1.Text + "''", conn);
conn.open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{

tbappname.Text = reader["Fname"].ToString();
reader.Close();
conn.Close();
}
else
{
string message="no record found";
reader.Close();
conn.Close();
}


如果您打算在Textbox控件中绑定文本,请使用下面的代码..您编写上面的代码的目的是从db中获取所有记录,并且它将一条记录绑定到TextBox控件,因此先前的值将被清除,新的值将出现在图片中.
使用以下代码

字符串连接,SQL;

连接= Properties.Settings.Default.cvmanagerConnectionString;
sql =从其中FileLoc =""+文件名+""的应用程序中选择FileLoc,Fname,Lname;

试试
{
SqlDataReader reader = null;
SqlConnection conn =新的SqlConnection(connection);
SqlCommand cmd =新的SqlCommand(sql,conn);

conn.Open();
//reader = cmd.ExecuteReader();

对象Fname = cmd.ExecuteScalar();

if(Fname!= null)
{
tbappname.Text = Fname.ToString();
}
//if(reader.HasRows)
//{
//同时(reader.Read())
//{
//tbappname.Text = reader ["Fname"].ToString();
//}
//}
}
catch(ex ex例外)
{
MessageBox.Show(ex.ToString());
}
终于
{
conn.Close()
}
If your planning to bind text in Textbox control please use the below code .. what your wrote the above code is to get all record from db and it will bind record one by one to TextBox control so the previous value will clear and new value will come to the pic.
Use the below code

String connection, sql;

connection = Properties.Settings.Default.cvmanagerConnectionString;
sql = "select FileLoc, Fname, Lname from apps where FileLoc = ''" + filename + "''";

try
{
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
//reader = cmd.ExecuteReader();

object Fname = cmd.ExecuteScalar();

if(Fname!=null)
{
tbappname.Text = Fname.ToString();
}
//if (reader.HasRows)
//{
//while (reader.Read())
// {
// tbappname.Text = reader["Fname"].ToString();
// }
//}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close()
}


检查db中是否有该特定文件名的数据...

请运行以下代码以测试天气是否超出了返回数据的范围,并让我知道.

字符串连接,SQL;

连接= Properties.Settings.Default.cvmanagerConnectionString;
sql =从FileLoc =""+ filename +""的应用程序中选择Fname;

试试
{
SqlDataReader reader = null;
SqlConnection conn =新的SqlConnection(connection);
SqlCommand cmd =新的SqlCommand(sql,conn);

conn.Open();
//reader = cmd.ExecuteReader();

对象Fname = cmd.ExecuteScalar();

if(Fname!= null)
{
Response.Write("alert(""+ Fname.ToString()+"''););
tbappname.Text = Fname.ToString();
}
}
catch(ex ex例外)
{
MessageBox.Show(ex.ToString());
}
终于
{
conn.Close()
}
check do you have data in db for that particular file name...

please run the below code for testing weather the obeject returning data or not and let me know .

String connection, sql;

connection = Properties.Settings.Default.cvmanagerConnectionString;
sql = "select Fname from apps where FileLoc = ''" + filename + "''";

try
{
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

conn.Open();
//reader = cmd.ExecuteReader();

object Fname = cmd.ExecuteScalar();

if(Fname!=null)
{
Response.Write("alert(''"+ Fname.ToString() +"'');");
tbappname.Text = Fname.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close()
}


这篇关于有人可以帮助我与SQL数据读取器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆