indexoutof范围异常在c#代码中未处理 [英] indexoutof range Exception was unhandled in c# code

查看:65
本文介绍了indexoutof范围异常在c#代码中未处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void getmaxScript()
        {

            string ConnectionStringIntellectServerDetails = "Data Source=" + txtServerNameIPIntellect.Text + ";Initial Catalog=" + lblSqlDatabase.Text + ";user id=" + txtUsernameIntellect.Text + ";password=" + txtPasswordIntellect.Text + "";
            SqlConnection conn = new SqlConnection(ConnectionStringIntellectServerDetails);
            conn.Open();

            SqlCommand cmmd = new SqlCommand("", conn);
            cmmd.CommandText = "CREATE TABLE tbl_Script([tblScript_Id] INTEGER CONSTRAINT PKeytblScript_Id Primary Key, [ScriptId] INTEGER)";
            if (conn.State == ConnectionState.Open)
            {
                try
                {
                    cmmd.ExecuteNonQuery();
                    MessageBox.Show("Add!");
                    var ScriptId = 0;
                    string sql = "Select Max(ScriptId) from tbl_Script";
                     SqlCommand cmd = new SqlCommand(sql, conn);
                     using (var reader = cmd.ExecuteReader())
                     {
                         if (reader["ScriptId"] == System.DBNull.Value)
                         {
                             ScriptId = 1;
                         }
                         else
                         {
                            ScriptId= ScriptId++;
                         }

                         globalScriptid = ScriptId.ToString();
                         string sqlmax = "insert into tbl_Script(ScriptId) values(" + globalScriptid+ ")";
                         SqlCommand cmd1 = new SqlCommand(sqlmax, conn);
                         cmd1.ExecuteNonQuery();
                     }
                }
                catch (SqlException expe)
                {
                    MessageBox.Show(expe.Message);
                    conn.Close();
                }      

            }
            else
            {
                MessageBox.Show("Error!");
            }
        }





以上是我的代码我得到的错误(indexoutof范围异常未处理)在线

if(reader [ScriptId] == System.DBNull.Value)请帮助我改变我做的比我的代码工作正确的方式请帮助



above is my code i an get error ( indexoutof range Exception was unhandled) on line
if (reader["ScriptId"] == System.DBNull.Value) pls help me wt change i do than my code work proper way pls help

推荐答案

您不会从查询中返回ScriptId列:

You don't return a column "ScriptId" from your query:
string sql = "Select Max(ScriptId) from tbl_Script";
 SqlCommand cmd = new SqlCommand(sql, conn);
 using (var reader = cmd.ExecuteReader())
 {
     if (reader["ScriptId"] == System.DBNull.Value)



你返回一个没有名字的计数。



但是还有一些其他的东西你需要看一下:

1)你不需要读者返回一个计数:任何单个值返回都可以用ExecuteScalar完成而不是ExecuteReader。

2)你的代码对于非常难以追踪的令人讨厌的间歇性错误是敞开的。请记住,您正在使用设计为多用途系统的SQL:因此,不同的PC很容易进入并使用您尝试的相同ID值。不要阅读最高价值并添加一个 - 这是您的应用程序在生产中摔倒的配方,但在开发中完美运行。请考虑使用Identity列或Guid列。


You return a count, which will have no name instead.

But there are a couple of other things here you need to look at:
1) You don't need a reader to return a count: any single value return can be done with an ExecuteScalar instead of ExecuteReader.
2) Your code is wide open to nasty intermittent bugs which are very hard to track down. Remember you are working with SQL which is designed as a multi-use4r system: so it is very easy for a different PC to "get in" and use the same ID value you are trying to. Don't "read the highest value and add one" - that is a recipe for your app falling over in production, but working perfectly in development. Consider using either an Identity column, or a Guid column instead.


用下面的字符串sql替换





string sql =从tbl_Script选择Max(ScriptId)作为ScriptId;
replace your string sql with below


string sql = "Select Max(ScriptId) as ScriptId from tbl_Script";


这篇关于indexoutof范围异常在c#代码中未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆