2015-05-30 26 views
-1

我得到這個錯誤:」初始化字符串的格式不符合從索引0開始的規範。「

Format of the initialization string does not conform to specification starting at index 0.

我的代碼是:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Data.SqlClient; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Security.Cryptography; 
using System.Data.OleDb; 
using System.Configuration; 

namespace LibrarySystem 
{ 
    public partial class PIDD : Form 
    { 
     public PIDD() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
      frmMed Med = new frmMed(); 
      Med.Show(); 

      var conn = new SqlConnection("ConnectionString"); 
      //var command = new SqlCommand("Patient.dbo.P_ID", connection); 
      var command = new SqlCommand("SELECT P_ID FROM Patient WHERE id='" + textBox1.Text + "",connection); 

      connection.Open(); 
      var reader = command.ExecuteReader(); 
      if (reader.Read()) 
      { 
       textBox1.Text = reader["id"].ToString(); 
      } 
      else 
      { 
       // No entry found 
      } 
      connection.Close(); 
     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
     } 

     public SqlConnection connection { get; set; } 
    } 
} 

我想寫已經存在於數據庫ID和檢查ID是否正確,,然後顯示它在明年fram ,,

+1

沒有什麼有意義這裏。問題是什麼?你有什麼錯誤信息(以及代碼中的哪些地方會發生?)什麼是frmMed,爲什麼在點擊時關閉PIDD表單?你在哪裏寫什麼? – Amit

+1

'SqlConnection(「ConnectionString」);'?它是你的實際代碼還是隨機假樣本?如果尋找調試幫助,你需要提供 - 代碼,確切的錯誤信息與它發生的位置,預期和觀察到的行爲。請注意,代碼的子文件格式可能會爲您賺取一些降價... –

回答

0

初始化字符串的格式不符合從索引0開始的規範。只是一個將錯誤的連接字符串賦予的ArgumentException消息3210:給它一個valid SQL Server connection string(即而不是"ConnectionString"),並且您將準備好繼續處理代碼中的其他問題。

僅供參考,你可以看到我快,微創演變你的代碼,以確認給定的錯誤消息的來源LINQPad 4:

enter image description here

相關問題