首先,我是C#的初学者,很抱歉,如果这个问题很容易(或没有),但我被封锁了。
我正在尝试使用该代码在我的数据库中找到记录:
public int chercherDansDataSet(string search)
{
DataRow[] foundRows;
String idSearch;
int result = 0;
int idKey = 0 ;
DataTable dt = ds.Tables[0];
string research = "last_name = '"+search+"'";
foundRows = dt.Select(research);
result = foundRows.Length;
if(result == 0)
{
MessageBox.Show("Aucun résultat");
return 1;
}
else
{
DataRow rowBack;
rowBack = foundRows[0];
idSearch = rowBack[0].ToString();
idKey = Int32.Parse(idSearch);
return idKey;
}
}
但每次我尝试时,我都会:
未找到任何记录
(我的代码中的法语中的“AucunRésultat”,if
获取result == 0
时的结果
当我调试时,我的数据表不是空的,所以我认为这不是问题:
我的目标是返回搜索到的行的ID ....
这是我的数据集配置,但我不认为问题来自此处:
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_Data_1");
con.Close();
return dat_set;
}
还有Form1Load,我的ds已设置:
private void Form1_Load(object sender, EventArgs e)
{
try
{
objConnect = new DatabaseConnection();
conString = Properties.Settings.Default.EmployeeConnectionString;
objConnect.connection_string = conString;
objConnect.Sql = Properties.Settings.Default.SQL;
ds = objConnect.GetConnection;
MaxRows = ds.Tables[0].Rows.Count;
NavigateRecords();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
我的班级“DatabaseConnection”:
class DatabaseConnection
{
private string sql_string;
private string strCon;
System.Data.SqlClient.SqlDataAdapter da_1;
public string Sql
{
set
{
sql_string = value;
}
}
public string connection_string
{
set { strCon = value; }
}
public System.Data.DataSet GetConnection
{
get { return MyDataSet(); }
}
public DataSet dat_set { get; private set; }
private System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
con.Open();
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_Data_1");
con.Close();
return dat_set;
}
public void UpdateDatabase(System.Data.DataSet ds)
{
System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
cb.DataAdapter.Update(ds.Tables[0]);
}
}
}
答案 0 :(得分:0)
首先,您必须将数据集的声明移动到表单的顶部,如:
public partial class Form1 : Form
{
System.Data.DataSet ds= new System.Data.DataSet();
private void Form1_Load(object sender, EventArgs e)
{
并从您的程序MyDataSet
中删除声明
然后在您的chercherDansDataSet
函数中使用变量dat_set
代替ds
:
DataTable dt = dat_set.Tables[0];
而不是
DataTable dt = ds.Tables[0];
尝试添加" []"给你变量和像:
这样的空格string research = "[last_name] = '" + search + "'";
foundRows = dt.Select(research);
result = foundRows.Length;
如果你没有" _"在您的数据表中尝试:
string research = "[last name] = '" + search + "'";
答案 1 :(得分:-1)
我建议你可以使用数据集来做,这里是示例代码:
ds = getUserName(); //search using parameterized query and return record found as ds
if (ds.Tables[0].Rows.Count > 0) //if record found more than 1 row, mean doesnt empty
{
return ds;
}
else
{
MessageBox.Show("Aucun résultat");
return 1;
}