c#中的SqlConnection错误索引0

时间:2016-05-09 09:41:06

标签: c# sqlconnection

在下面的代码中,当我尝试连接到连接字符串时,我看到此错误,这会出现错误

初始化字符串的格式不符合从索引0开始的规范。

我该如何解决? 在下面的代码中,当我尝试连接到连接字符串时,我看到此错误,这会出现错误

初始化字符串的格式不符合从索引0开始的规范。

我该如何解决?

namespace InsertUserApp
{
    public partial class Home : Form
    {
        class ComboItemExample
        {
            public string DisplayString { get; set; }
            public string ConnectionString { get; set; }

            public override string ToString() { return DisplayString; }
        }
        private string connstring = "<default connection>";

        public Home()
        { 
            InitializeComponent();

            var srv1 = new ComboItemExample { DisplayString = "Srv1", ConnectionString = "Data Source=tcp:10.0.0.110;Initial Catalog=Database1;User ID=user;Password=pass;" };
            cmbSrv.Items.Add(srv1);

            var srv2 = new ComboItemExample { DisplayString = "Srv2", ConnectionString = "Data Source=tcp:10.1.0.11;Initial Catalog=Database2;User ID=user;Password=pass;" };
            cmbSrv.Items.Add(srv2);

            var srv3 = new ComboItemExample { DisplayString = "Srv3", ConnectionString = "Data Source=tcp:10.1.0.15;Initial Catalog=Database3;User ID=user;Password=pass" };
            cmbSrv.Items.Add(srv3);

        }


            private void txtID_TextChanged(object sender, EventArgs e)
    { 
            using (SqlConnection connection = new SqlConnection(connstring))
            {
                /*if (String.IsNullOrEmpty(txtID.Text))
                {
                    MessageBox.Show("Plotesoni fushat UnID", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }
                else
                {*/
                SqlCommand command1 =
                new SqlCommand("select * from Table1 WHERE Tag = 'User' and UnId = '" + txtID.Text + "'", connection);
                SqlCommand command2 =
                new SqlCommand("select * from Table1 WHERE Tag = 'Pass' and UnId = '" + txtID.Text + "'", connection);
                SqlCommand command3 =
                new SqlCommand("select * from Table1 WHERE Tag = 'Shifra1' and UnId = '" + txtID.Text + "'", connection);
                SqlCommand command4 =
                SqlCommand command5 =
                new SqlCommand("select * from Table2 WHERE UnId = '" + txtID.Text + "'", connection);
                connection.Open();

                SqlDataReader read1 = command1.ExecuteReader();


                while (read1.Read())
                {
                    txtID.Text = (read1["UnId"].ToString());
                    txtUser.Text = (read1["Value"].ToString());

                }
                read1.Close();
                SqlDataReader read2 = command2.ExecuteReader();
                while (read2.Read())
                {
                    txtPass.Text = (read2["Value"].ToString());
                }
                read2.Close();
                SqlDataReader read3 = command3.ExecuteReader();
                while (read3.Read())
                {
                    txtOrg.Text = (read3["Value"].ToString());
                }
                read3.Close();
                SqlDataReader read5 = command5.ExecuteReader();
                while (read5.Read())
                {
                    txtgrp.Text = (read5["shifra1"].ToString());
                }
                read5.Close();
            }
        }


        }
    }
}

1 个答案:

答案 0 :(得分:0)

正如我通过互联网审查的那样,我认为问题在于这行代码

有什么建议吗?

if (entity.IsValidForAddOrUpdate())
{
    db.Set<Entity>().Add(entity);
    db.SaveChanges()
}
else throw new DbValidationException("Entity failed validation due to rule xyz.");