插入到sql表中,通过csharp输入到某些文本框,组合框和datetimepicker的值

时间:2014-04-09 07:57:00

标签: c# sql sql-server

我真的很陌生,并试图自己学习这些东西。我需要在SQL数据库的Findings表中插入一些数据,这些数据将由用户输入到某些文本框,组合框和datetimepicker。我的方法如下。

 private void SaveAsNewFinding_Click(object sender, EventArgs e)
      {
          DialogResult dr = MessageBox.Show("You are About to Save Your Finding \nAre You Sure You Want to Do this?", "Save Findings", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
      if (dr == DialogResult.Yes)
      {


          Edit.Enabled = true;
          Back.Enabled = true;
          Next.Enabled = true;
          Save.Visible = false;
          Cancel.Visible = false;
          Find_FC_ID();

          String FByTitle ="INSERT INTO dbo.Findings 
                            (Findings.Findings_Title, Findings.Findings_Description,Findings.Findings_Rating,Findings.Findings_Implication,Findings.Findings_Recomendation,Findings.Findings_ManagementComment,Findings.Findings_ManagerResponsible,Findings.Findings_CompletionDate,Findings.Findings_Status)
                            VALUES (@FindingsTitle,@FindingsDescription,@FindingsRating,@FindingsImplication,@FindingsRecomendation,@FindingsManagementComment,@FindingsManagerResponsible,@FindingsCompletionDate,@FindingsStatus)";






          SqlCommand cmd = new SqlCommand(FByTitle, con.main_connect());
          cmd.Parameters.AddWithValue("@FindingsTitle",Findings_Title_TextBox.Text);
          cmd.Parameters.AddWithValue("@FindingsDescription",Findings_Description_TextBox.Text);
          cmd.Parameters.AddWithValue("@FindingsRating",Findings_Rating_ComboBox.Text);
          cmd.Parameters.AddWithValue("@FindingsImplication",Findings_Implication_TextBox.Text);
          cmd.Parameters.AddWithValue("@FindingsRecommendation",Findings_Recommendation_TextBox.Text);
          cmd.Parameters.AddWithValue("@FindingsManagementComment",Findings_Comment_TextBox.Text);
          cmd.Parameters.AddWithValue("@FindingsManagerResponsible",Findings_Responsibleperson_TextBox.Text);
          cmd.Parameters.AddWithValue("@FindingsCompletionDate",(Convert.ToDateTime(dateTimePicker1.Text)).ToString("MM-dd-yyyy"));
          cmd.Parameters.AddWithValue("@FindingsStatus",Findings_Status_ComboBox.Text);



          cmd.ExecuteNonQuery(); 




          //  reader.Read();

          //reader.Close();
          Findings_Title_TextBox.ReadOnly = true;
          Findings_Description_TextBox.ReadOnly = true;
          //Findings_Rating_ComboBo
          Findings_Implication_TextBox.ReadOnly = true;
          Findings_Recommendation_TextBox.ReadOnly = true;
          Findings_Comment_TextBox.ReadOnly = true;
          Findings_Responsibleperson_TextBox.ReadOnly = true;
          dateTimePicker1.Enabled = false;
          Findings_Status_ComboBox.Enabled = false;
          Findings_Rating_ComboBox.Enabled = false;
          treeView1.Visible = false;


      }

      else if (dr == DialogResult.Cancel)
      {
          Edit.Enabled = true;
          Back.Enabled = true;
          Next.Enabled = true;
          Save.Visible = false;
          Cancel.Visible = false;

          Display(F_ID);

      }

      else if (dr == DialogResult.No)
      {


      }

  }

此方法给出了以下错误,我不知道如何解决它们。

Error   1   Newline in constant Line-484(line which contains INSERT INTO dbo.Findings)
Error   2   Newline in constant Line-486(Line which contain the VALUES of the query)
Error   3   ; expected Line-485 (Line which contain the columns of the table)
Error   4   ; expected Line-486 Line-486(Line which contain the VALUES of the query)
Error   5   ; expected Line-486 Line-486(Line which contain the VALUES of the query)

根据我所遵循的书,学习以上理论的错误建议将不适用。 感谢您帮助解决问题。因为我是这门语言的新手,所以要善意地澄清一下。

干杯。

2 个答案:

答案 0 :(得分:1)

C#中有两种不同类型的字符串。你可以阅读它here

解决字符串的代码更改类型中的问题

  string FByTitle =@"INSERT INTO dbo.Findings 
                            (Findings.Findings_Title, Findings.Findings_Description,Findings.Findings_Rating,Findings.Findings_Implication,Findings.Findings_Recomendation,Findings.Findings_ManagementComment,Findings.Findings_ManagerResponsible,Findings.Findings_CompletionDate,Findings.Findings_Status)
                            VALUES (@FindingsTitle,@FindingsDescription,@FindingsRating,@FindingsImplication,@FindingsRecomendation,@FindingsManagementComment,@FindingsManagerResponsible,@FindingsCompletionDate,@FindingsStatus)";

答案 1 :(得分:0)

您不能像在String FByTitle字符串中那样插入换行符。

如果你想避免排长队,你应该这样写:

String FByTitle ="INSERT INTO dbo.Findings " + 
                "(Findings.Findings_Title, Findings.Findings_Description,Findings.Findings_Rating," + 
                "Findings.Findings_Implication,Findings.Findings_Recomendation,Findings.Findings_ManagementComment," + 
                "Findings.Findings_ManagerResponsible,Findings.Findings_CompletionDate,Findings.Findings_Status) " + 
                " VALUES (@FindingsTitle,@FindingsDescription,@FindingsRating,@FindingsImplication," + 
                "@FindingsRecomendation,@FindingsManagementComment,@FindingsManagerResponsible,@FindingsCompletionDate,@FindingsStatus)";

甚至更短。每个细分都在一条线上,你可以用一个' +'登录。

这不是VB; - )