ExecuteScalar() を使用してデータベースから整数を返そうとしています。ただし、データベース自体でクエリを実行すると、正しい答えが得られ、c# は常に 0 (Null) を返します。id = Convert.ToInt32(command.ExecuteScalar());
追加する前に、NULLが処理されていることを確認するように指示するエラーが表示されるため、nullが返されることはわかっています。私はそれが3 btwを返すことを期待しています。
private int getFamilyId()
{
int id = 0;
using (SqlConnection connection = new SqlConnection(Globaldata.ConnectionString))
{
using (SqlCommand command = new SqlCommand())
{
string sqlString = @"SELECT [Id] FROM [dbo].[FamilyDetails];";
command.Connection = connection;
command.CommandText = sqlString;
try
{
connection.Open();
id = Convert.ToInt32(command.ExecuteScalar());
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK);
}
finally
{
connection.Close();
}
return id;
}
}
}