内个。。。我写了个函数,不知道咋错了,能帮看不?谢谢那!
函数是:
private static string getstr(string str, string partyp, string flg)
{
string connectionString = ConfigurationManager.ConnectionStrings[ "ConnectionString "].ConnectionString;//得到配置节
SqlConnection DBConnection = new SqlConnection(connectionString);//创建连接
DBConnection.Open();
char[] delimiterChars = { ', ' };
string[] strs = str.Split(delimiterChars);//数组存结果
string devstring = " ";
string result = " ";
for (int j = 0; j < strs.Length; j++)
{
if (strs[j] != " ")
{
string strstr = "select para_drpt from sysconf where para_typ= ' " + partyp + " ' and para_cd= ' " + strs[j] + " ' ";
if (flg == "ss " || flg == "H6 ")
{
strstr += " ' and subcd_flg= ' " + flg + " ' ";
}
SqlCommand strcmd = new SqlCommand(strstr, DBConnection);
SqlDataReader strdr = strcmd.ExecuteReader();
while (strdr.Read())
{
devstring = strdr.GetValue(strdr.GetOrdinal( "para_drpt ")).ToString().TrimEnd();
}
strdr.Close();
string[] devstrings = new string[strs.Length];
devstrings[j]=devstring;
if (devstrings[j] != " ")
{
result += devstrings[j]+ ", ";
}
}
}
DBConnection.Close();
return result;
}
调用就是:
Label1.Text = getstr(tstatstr, "H5 ", " ");
return result;得值是完全正确的,不晓得为啥以调用就错鸟
报错:System.NullReferenceException: Object reference not set to an instance of an object.
Line 154: l1.Text = getstr(tstatstr, "H5 ", " ");
[解决办法]
把static 去掉
[解决办法]
传一个参数,参数置空