我制作在线应用程序以在MS SQL中存储员工信息,我使用Disconnected模型,然后我将我的课程设为" DataSet配置"并在Form_Load事件中调用其中的方法。 这是班级
public static string constr = ConfigurationManager.ConnectionStrings [" cnn1"]。ConnectionString;
public static DataSet GetDataSet(string stored_name, string table_name, params SqlParameter[] prmarr)
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(stored_name, con);
foreach (SqlParameter prm in prmarr)
{
cmd.Parameters.Add(prm);
}
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, table_name);
return ds;
我在" Web.Config"
中设置了连接字符串到目前为止所有的事情都还可以,但是当我将数据插入" DataSet" 出现错误消息 你调用的对象是空的。
这是我输入插入按钮
的代码 int max = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
if ((int)dr["Emp_Id"] > max)
{
max = (int)dr["Emp_Id"];
}
}
DataRow drnew = ds.Tables["x"].NewRow();
drnew["Emp_Id"] = ++max;
drnew["First_Name"] = txtfname.Text;
drnew["Last_Name"] = txtlname.Text;
drnew["Country"] = drpboxcoun.SelectedValue;
drnew["Email"] = txtemail.Text;
drnew["Experience_Years"] = txtexper.Text;
drnew["Current_Salary"] = txtcursal.Text;
drnew["Expected_Salary"] = txtexpsal.Text;
ds.Tables["x"].Rows.Add(drnew);
问题出在哪里!!
答案 0 :(得分:0)
我同意granadaCoder的数据类型问题
添加try catch或跟踪它...
当您要添加到此数据集时,这些“FORM feelds”会保存到DB?
这里有一些我做的代码
public class myDAL
{
/// <summary>
/// Collection of SqlParameter
/// </summary>
public System.Collections.Generic.List<SqlParameter> __paramList = new System.Collections.Generic.List<SqlParameter>();
/// <summary>
/// AddParameter - myDAL.AddParameter(@"@_" + ParamName.ToString(), value, sqlDatatype, System.Data.ParameterDirection.Input);
/// </summary>
/// <param name="_paramname"></param>
/// <param name="_paramvalue"></param>
/// <param name="_paramtype"></param>
/// <param name="_ParameterDirection"></param>
/// <remarks></remarks>
public void AddParameter(string _paramname, object _paramvalue, System.Data.SqlDbType _paramtype, ParameterDirection _ParameterDirection)
{
SqlParameter param = new SqlParameter(_paramname, _paramtype);
param.Direction = _ParameterDirection;
param.Value = _paramvalue;
__paramList.Add(param);
}
/// <summary>
/// ParametersClear __paramList.Clear();
/// </summary>
public void ParametersClear()
{
__paramList.Clear();
}
public dataset Here returnDataSetSnippet(out bool hasError){
//JUST REDO WITH DATASET
using (SqlConnection conn = new SqlConnection(getConnection.ConnectionString()))
{
using (SqlCommand mySqlCommand = new System.Data.SqlClient.SqlCommand())
{
try
{
conn.Open();
mySqlCommand.Connection = conn;
mySqlCommand.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter p in __paramList)
{
mySqlCommand.Parameters.Add(p);
}
SqlDataReader dbReader = mySqlCommand.ExecuteReader();
if (dbReader.Read())
{
c1 = dbReader["c1"].ToString();
c2 = dbReader["c2"].ToString();
hasError = false;
}
dbReader.Close();
}
catch (Exception ex)
{
hasError = true;
//LOG HERE
}
} // end using
}// end using
return myDataSet;
}
}
这是用法
public myDAL = new myDAL();
System.Data.SqlDbType sqlDatatype = (System.Data.SqlDbType)Enum.Parse(typeof(System.Data.SqlDbType), EnumHelper.EnumDescription(Req_Param), true);
myDAL.AddParameter(@"@_" + Req_Param.ToString(), value, sqlDatatype, System.Data.ParameterDirection.Input);
我有类型
的枚举 public enum require_params
{
[Description("Int")]
UserStatus,
[Description("TinyInt")]
Mode
}
public static class EnumHelper
{
public enum Speed
{
[Description("5 metters per second")]
Five = 5,
[Description("10 metters per second")]
Ten = 10,
[Description("15 metters per second")]
Fifteen = 15,
[Description("20 metters per second")]
Twenty = 20,
//[Description("25 metters per second")]
TwentyFive = 25,
[Description("30 metters per second")]
Thirty = 30
}
/// <summary>
/// get the string value of Enum Attribute
/// </summary>
/// <param name="EnumConstant"></param>
/// <returns>
/// string enumDesctiption = EnumHelper.EnumDescription(EnumHelper.Speed.Thirty);
/// enumDesctiption = EnumHelper.EnumDescription(DayOfWeek.Monday); when there is no desc returns as string the ENUM property
/// </returns>
public static string EnumDescription(Enum EnumConstant)
{
System.Reflection.FieldInfo fi = EnumConstant.GetType().GetField(EnumConstant.ToString());
DescriptionAttribute[] aattr = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (aattr.Length > 0)
{
return aattr[0].Description;
}
else
{
return EnumConstant.ToString();
}
}
}