C#报错Not all Code paths return一个值,做查询的时候只用returns值就好
C# error Not all Code paths return a value, when doing just the quesry returns value just fine
我需要用数据库中的信息填充数据网格,我有一个数据访问 class 和正常的 form.cs file.When 我自己做查询 returns 值很好,但由于某些原因,当 运行 在代码中通过 sp 时它没有。
我在 WindowsForms 中遇到了这个错误,我不太明白我做错了什么。我将从展示我的数据访问开始 class
public DataTable Sp_Teste()
{
try
{
ManageConnectionStateIbml1();
SqlCommand command = new SqlCommand("SP_SELECT_DOCS_WHERE_JOBID_STATUS",
connectionIBML1);
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
Console.WriteLine("Erro: " + ex.Message);
}
finally
{
ManageConnectionStateIbml1();
connectionIBML1.Close();
}
}
接下来是我的 Form.cs,因为我有 3 个数据网格视图,所以我将它们与区域分开。
#region DatagridViewDocumentos
dataGridViewDocumentos.DataSource = null;
dataGridViewDocumentos.Columns.Clear();
dataGridViewDocumentos.Refresh();
DataTable dt = da.Sp_Teste();
dataGridViewDocumentos.DataSource = dt;
dataGridViewDocumentos.ColumnCount = 4;
DataGridViewColumn columnCaixa = dataGridViewDocumentos.Columns[0];
columnCaixa.Width = 50;
DataGridViewColumn columnDOC = dataGridViewDocumentos.Columns[1];
columnDOC.Width = 50;
DataGridViewColumn columnID = dataGridViewDocumentos.Columns[2];
columnID.Width = 50;
DataGridViewColumn columnPROC = dataGridViewDocumentos.Columns[3];
columnPROC.Width = 50;
dataGridViewDocumentos.Columns[0].Name = "Caixa";
dataGridViewDocumentos.Columns[1].Name = "DOC";
dataGridViewDocumentos.Columns[2].Name = "ID";
dataGridViewDocumentos.Columns[3].Name = "Proc";
#endregion
望大侠指正,谢谢
在函数末尾添加Return语句。
finally
{
ManageConnectionStateIbml1();
connectionIBML1.Close();
}
return null;
public DataTable Sp_Teste()
{
DataTable results = null;
try
{
ManageConnectionStateIbml1();
SqlCommand command = new SqlCommand("SP_SELECT_DOCS_WHERE_JOBID_STATUS",
connectionIBML1);
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
results = ds.Tables[0];
}
catch (Exception ex)
{
Console.WriteLine("Erro: " + ex.Message);
}
finally
{
ManageConnectionStateIbml1();
connectionIBML1.Close();
}
return results;
}
我认为你只需要在将数据分配给它的数据源后调用 gridview 的 DataBind() 方法 属性。
#region DatagridViewDocumentos
dataGridViewDocumentos.DataSource = null;
dataGridViewDocumentos.DataBind();
dataGridViewDocumentos.Columns.Clear();
dataGridViewDocumentos.Refresh();
DataTable dt = da.Sp_Teste();
dataGridViewDocumentos.DataSource = dt;
dataGridViewDocumentos.DataBind();
dataGridViewDocumentos.ColumnCount = 4;
DataGridViewColumn columnCaixa = dataGridViewDocumentos.Columns[0];
columnCaixa.Width = 50;
DataGridViewColumn columnDOC = dataGridViewDocumentos.Columns[1];
columnDOC.Width = 50;
DataGridViewColumn columnID = dataGridViewDocumentos.Columns[2];
columnID.Width = 50;
DataGridViewColumn columnPROC = dataGridViewDocumentos.Columns[3];
columnPROC.Width = 50;
dataGridViewDocumentos.Columns[0].Name = "Caixa";
dataGridViewDocumentos.Columns[1].Name = "DOC";
dataGridViewDocumentos.Columns[2].Name = "ID";
dataGridViewDocumentos.Columns[3].Name = "Proc";
#endregion
当您清除列然后绑定数据时,所以我认为这是造成问题的原因,只是想一想从网格中删除所有列后,如何访问它们,OS先设置列再绑定数据
#region DatagridViewDocumentos
dataGridViewDocumentos.DataSource = null;
dataGridViewDocumentos.Columns.Clear();
dataGridViewDocumentos.Refresh();
DataGridViewColumn columnCaixa = new DataGridViewColumn("Caixa");
columnCaixa.Width = 50;
DataGridViewColumn columnDOC = new DataGridViewColumn("DOC");
columnDOC.Width = 50;
DataGridViewColumn columnID = new DataGridViewColumn("ID");
columnID.Width = 50;
DataGridViewColumn columnPROC = new DataGridViewColumn("Proc");
columnPROC.Width = 50;
dataGridViewDocumentos.columns.Add(columnCaixa);
dataGridViewDocumentos.columns.Add(columnDOC );
dataGridViewDocumentos.columns.Add(columnID );
dataGridViewDocumentos.columns.Add(columnPROC );
dataGridViewDocumentos.ColumnCount = 4;
DataTable dt = da.Sp_Teste();
dataGridViewDocumentos.DataSource = dt;
#endregion
我需要用数据库中的信息填充数据网格,我有一个数据访问 class 和正常的 form.cs file.When 我自己做查询 returns 值很好,但由于某些原因,当 运行 在代码中通过 sp 时它没有。 我在 WindowsForms 中遇到了这个错误,我不太明白我做错了什么。我将从展示我的数据访问开始 class
public DataTable Sp_Teste()
{
try
{
ManageConnectionStateIbml1();
SqlCommand command = new SqlCommand("SP_SELECT_DOCS_WHERE_JOBID_STATUS",
connectionIBML1);
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
Console.WriteLine("Erro: " + ex.Message);
}
finally
{
ManageConnectionStateIbml1();
connectionIBML1.Close();
}
}
接下来是我的 Form.cs,因为我有 3 个数据网格视图,所以我将它们与区域分开。
#region DatagridViewDocumentos
dataGridViewDocumentos.DataSource = null;
dataGridViewDocumentos.Columns.Clear();
dataGridViewDocumentos.Refresh();
DataTable dt = da.Sp_Teste();
dataGridViewDocumentos.DataSource = dt;
dataGridViewDocumentos.ColumnCount = 4;
DataGridViewColumn columnCaixa = dataGridViewDocumentos.Columns[0];
columnCaixa.Width = 50;
DataGridViewColumn columnDOC = dataGridViewDocumentos.Columns[1];
columnDOC.Width = 50;
DataGridViewColumn columnID = dataGridViewDocumentos.Columns[2];
columnID.Width = 50;
DataGridViewColumn columnPROC = dataGridViewDocumentos.Columns[3];
columnPROC.Width = 50;
dataGridViewDocumentos.Columns[0].Name = "Caixa";
dataGridViewDocumentos.Columns[1].Name = "DOC";
dataGridViewDocumentos.Columns[2].Name = "ID";
dataGridViewDocumentos.Columns[3].Name = "Proc";
#endregion
望大侠指正,谢谢
在函数末尾添加Return语句。
finally
{
ManageConnectionStateIbml1();
connectionIBML1.Close();
}
return null;
public DataTable Sp_Teste()
{
DataTable results = null;
try
{
ManageConnectionStateIbml1();
SqlCommand command = new SqlCommand("SP_SELECT_DOCS_WHERE_JOBID_STATUS",
connectionIBML1);
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
results = ds.Tables[0];
}
catch (Exception ex)
{
Console.WriteLine("Erro: " + ex.Message);
}
finally
{
ManageConnectionStateIbml1();
connectionIBML1.Close();
}
return results;
}
我认为你只需要在将数据分配给它的数据源后调用 gridview 的 DataBind() 方法 属性。
#region DatagridViewDocumentos
dataGridViewDocumentos.DataSource = null;
dataGridViewDocumentos.DataBind();
dataGridViewDocumentos.Columns.Clear();
dataGridViewDocumentos.Refresh();
DataTable dt = da.Sp_Teste();
dataGridViewDocumentos.DataSource = dt;
dataGridViewDocumentos.DataBind();
dataGridViewDocumentos.ColumnCount = 4;
DataGridViewColumn columnCaixa = dataGridViewDocumentos.Columns[0];
columnCaixa.Width = 50;
DataGridViewColumn columnDOC = dataGridViewDocumentos.Columns[1];
columnDOC.Width = 50;
DataGridViewColumn columnID = dataGridViewDocumentos.Columns[2];
columnID.Width = 50;
DataGridViewColumn columnPROC = dataGridViewDocumentos.Columns[3];
columnPROC.Width = 50;
dataGridViewDocumentos.Columns[0].Name = "Caixa";
dataGridViewDocumentos.Columns[1].Name = "DOC";
dataGridViewDocumentos.Columns[2].Name = "ID";
dataGridViewDocumentos.Columns[3].Name = "Proc";
#endregion
当您清除列然后绑定数据时,所以我认为这是造成问题的原因,只是想一想从网格中删除所有列后,如何访问它们,OS先设置列再绑定数据
#region DatagridViewDocumentos
dataGridViewDocumentos.DataSource = null;
dataGridViewDocumentos.Columns.Clear();
dataGridViewDocumentos.Refresh();
DataGridViewColumn columnCaixa = new DataGridViewColumn("Caixa");
columnCaixa.Width = 50;
DataGridViewColumn columnDOC = new DataGridViewColumn("DOC");
columnDOC.Width = 50;
DataGridViewColumn columnID = new DataGridViewColumn("ID");
columnID.Width = 50;
DataGridViewColumn columnPROC = new DataGridViewColumn("Proc");
columnPROC.Width = 50;
dataGridViewDocumentos.columns.Add(columnCaixa);
dataGridViewDocumentos.columns.Add(columnDOC );
dataGridViewDocumentos.columns.Add(columnID );
dataGridViewDocumentos.columns.Add(columnPROC );
dataGridViewDocumentos.ColumnCount = 4;
DataTable dt = da.Sp_Teste();
dataGridViewDocumentos.DataSource = dt;
#endregion