在C#中将数据类型nvarchar转换为bigint时出错

时间:2015-03-17 07:10:27

标签: c# sql-server

存储过程

   ALTER PROCEDURE [dbo].[spInsertLoanMaster]
   @TAc_no bigint
   ,@TAcc_no bigint
  ,@TAc_name nvarchar(50)
  ,@TV_no bigint
  ,@TJlg_no bigint
  ,@TPost_no bigint
  ,@TPhone_no1 bigint
  ,@TAct_no bigint
  ,@TLoan_amt bigint
  ,@TLoan_date date
  ,@TInt_rate bigint
  ,@TPay_mode nvarchar(max)
  ,@TPrin_inst nvarchar(max)
  ,@TGuar_name1 nvarchar(50)
  ,@TGuar_name2 nvarchar(50)
  ,@TChq_no bigint
 ,@TChq_date date
  ,@TGurAdd1 nvarchar(100)
  ,@TGurAdd2 nvarchar(100)
  ,@TGurPhone1 nvarchar(100)
  ,@TGurPhone2 nvarchar(100)
  ,@TPeriodInYear nvarchar(100)
  ,@TPeriod nvarchar(100)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

-- Insert statements for procedure here
insert Loan_Master (Ac_no,Acc_no , Ac_name,V_no,Jlg_no,Post_no,Act_no,phone_no1,Guar_name1,GurAdd1,GurPhone1,Guar_name2,GurAdd2,GurPhone2,Loan_amt,Loan_date ,Int_rate,PeriodInYear,Pay_mode,Prin_inst,Chq_date ,Chq_no,Period)values
(
   @TAc_no
  ,@TAcc_no
  ,@TAc_name
  ,@TV_no
  ,@TJlg_no
  ,@TPost_no
  ,@TPhone_no1
  ,@TAct_no
  ,@TLoan_amt
  ,@TLoan_date
  ,@TInt_rate
  ,@TPay_mode
  ,@TPeriodInYear 
  ,@TPrin_inst
  ,@TGuar_name1
  ,@TGuar_name2
  ,@TChq_no
  ,@TChq_date
  ,@TGurAdd1
  ,@TGurAdd2
  ,@TGurPhone1
  ,@TGurPhone2,
   @TPeriod)

END

调用此程序

    public void getInsert(Int64 Acno,Int64 Accno, string Acname, Int64 Vno, Int64 Jlgno, Int64 Postno, Int64 Actno, Int64 phoneno1, string Guarname1, string Guradd1, string  GurPh1, string Guarname2, string Guradd2, string  GurPh2,Int64 Loanamt, DateTime Loandate,Int64 Intrate, string  PeriodYear, string Paymode, string   Prininst, Int64 Chqno,DateTime Chqdate,string   period1)
    {
            conobj.getconnection();
            conobj.con.Open();
            conobj.cmd = new SqlCommand("spInsertLoanMaster", conobj.con);
            conobj.cmd.CommandType = CommandType.StoredProcedure;
            conobj.cmd.Parameters.Add("@TAc_no", SqlDbType.BigInt).Value = Acno;
            conobj.cmd.Parameters.Add("@TAcc_no", SqlDbType.BigInt).Value = Accno;
            conobj.cmd.Parameters.Add("@TAc_name", SqlDbType.NVarChar).Value = Acname;
            conobj.cmd.Parameters.Add("@TV_no", SqlDbType.BigInt).Value = Vno;
            conobj.cmd.Parameters.Add("@TJlg_no", SqlDbType.BigInt).Value = Jlgno;
            conobj.cmd.Parameters.Add("@TPost_no", SqlDbType.BigInt).Value = Postno;
            conobj.cmd.Parameters.Add("@TAct_no", SqlDbType.BigInt).Value = Actno;
            conobj.cmd.Parameters.Add("@TPhone_no1", SqlDbType.BigInt).Value = phoneno1;
            conobj.cmd.Parameters.Add("@TGuar_name1", SqlDbType.NVarChar).Value = Guarname1;
            conobj.cmd.Parameters.Add("@TGurAdd1", SqlDbType.NVarChar).Value = Guradd1;
            conobj.cmd.Parameters.Add("@TGurPhone1", SqlDbType.NVarChar).Value = GurPh1;
            conobj.cmd.Parameters.Add("@TGuar_name2", SqlDbType.NVarChar).Value = Guarname2;
            conobj.cmd.Parameters.Add("@TGurAdd2", SqlDbType.NVarChar).Value = Guradd2;
            conobj.cmd.Parameters.Add("@TGurPhone2", SqlDbType.NVarChar).Value = GurPh2;
             conobj.cmd.Parameters.Add("@TLoan_date", SqlDbType.DateTime).Value = Loandate; 
            conobj.cmd.Parameters.Add("@TLoan_amt", SqlDbType.BigInt).Value = Loanamt;
            conobj.cmd.Parameters.Add("@TInt_rate", SqlDbType.BigInt).Value = Intrate;
            conobj.cmd.Parameters.Add("@TPeriodInYear", SqlDbType.NVarChar).Value = PeriodYear;
            conobj.cmd.Parameters.Add("@TPay_mode", SqlDbType.NVarChar).Value = Paymode;
            conobj.cmd.Parameters.Add("@TPrin_inst", SqlDbType.NVarChar ).Value = Prininst;
            conobj.cmd.Parameters.Add("@TChq_no", SqlDbType.BigInt).Value = Chqno;
                conobj.cmd.Parameters.Add("@TChq_date", SqlDbType.DateTime).Value = Chqdate;
            conobj.cmd.Parameters.Add("@TPeriod", SqlDbType.NVarChar  ).Value = period1;
            conobj.cmd.ExecuteNonQuery();
            conobj.con.Close();

    }

表格编码

   private void btnNewSave_Click(object sender, EventArgs e)
    {
        string s = "";
        if (CMB_VillageNum.SelectedIndex >= 0)
        {
            s = CMB_VillageNum.Items[CMB_VillageNum.SelectedIndex].ToString ();
        }
        string s1 = "";
        if (CMB_JLGNum.SelectedIndex >= 0)
        {
            s1 = CMB_JLGNum.Items[CMB_JLGNum.SelectedIndex].ToString();
        }
        string s2 = "";
        if (CMB_ActiviNum.SelectedIndex >= 0)
        {
            s2 = CMB_ActiviNum.Items[CMB_ActiviNum.SelectedIndex].ToString();
        }
        string s3 = "";
        if (CMB_PostNum.SelectedIndex >= 0)
        {
            s3 = CMB_PostNum.Items[CMB_PostNum.SelectedIndex].ToString();
        }
        string s4 = "";
        if (CMB_Period.SelectedIndex >= 0)
        {
            s4 = CMB_Period.Items[CMB_Period.SelectedIndex].ToString();
        }

        //if (CMB_Period.SelectedIndex == 1)
        //{
       //string dateof = this.dateTimePicker1.Text;
       //string dateto =this. dateTimePicker2.Text;
        //objLoan.getInsert(Convert.ToInt64(txtAccNo.Text), txtName.Text, Convert.ToInt64(CMB_VillageNum.SelectedValue), Convert.ToInt64(CMB_JLGNum.SelectedValue), Convert.ToInt64(CMB_PostNum.SelectedValue)
        //                 , Convert.ToInt64(CMB_ActiviNum.SelectedValue), Convert.ToInt64(txtPhone.Text), txtGurName1.Text, txtGurName2.Text, txtPhoneNum1.Text, txtPhoneNum2.Text, txtGur1Addresss.Text, txtGur2Addresss.Text, Convert.ToDateTime(dateTimePicker1.Text), Convert.ToInt64(txtLoanAmt.Text), Convert.ToInt64(txtRateOfInter.Text)
        //                 , "0", txtPaymentMode.Text, txtPriInstall.Text, Convert.ToInt64(txtChequeNo.Text), Convert.ToDateTime(dateTimePicker2.Text), txtPeroid.Text);
        //dateTimePicker1.Value.ToString("dd//MM//yyyy");
        //dateTimePicker2.Value.ToString("dd//MM//yyyy");

            objLoan.getInsert(Convert.ToInt64(txtAccNo.Text.Trim()), Convert.ToInt64(lblAccNum.Text.Trim()), txtName.Text, Convert.ToInt64(s.Trim()), Convert.ToInt64(s1.Trim()), Convert.ToInt64(s3.Trim())
                , Convert.ToInt64(s2.Trim()), Convert.ToInt64(txtPhone.Text.Trim()), txtGurName1.Text, txtGur1Addresss.Text
                                    , txtPhoneNum1.Text, txtGurName2.Text, txtGur2Addresss.Text, txtPhoneNum2.Text
                                    , Convert.ToInt64(txtLoanAmt.Text.Trim()), Convert .ToDateTime (dateTimePicker1 .Text ),Convert.ToInt64(txtRateOfInter.Text.Trim()),(s4.Trim()), txtPaymentMode.Text,
                                     txtPriInstall.Text, Convert.ToInt64(txtChequeNo.Text.Trim()), Convert .ToDateTime (dateTimePicker2 .Text ),txtPeroid.Text);

我尝试使用usp_TimesheetsAuditsLoadAllbyId 42747,NULL命令调用此过程。

但我总是收到错误

  

Msg 8114,Level 16,State 5,Procedure   usp_TimesheetsAuditsLoadAllById,第9行转换数据类型时出错   varchar to bigint。

TimesheetsAudits表的ID是bigint类型。我尝试了几种类型的转换和演员表,但我现在真的卡住了。

2 个答案:

答案 0 :(得分:1)

Insert查询和Value查询具有不同的Parameters顺序,这导致了Insert语句的问题。试试这个Insert查询

ALTER PROCEDURE [dbo].[spInsertLoanMaster]
   @TAc_no bigint
   ,@TAcc_no bigint
  ,@TAc_name nvarchar(50)
  ,@TV_no bigint
  ,@TJlg_no bigint
  ,@TPost_no bigint
  ,@TPhone_no1 bigint
  ,@TAct_no bigint
  ,@TLoan_amt bigint
  ,@TLoan_date date
  ,@TInt_rate bigint
  ,@TPay_mode nvarchar(max)
  ,@TPrin_inst nvarchar(max)
  ,@TGuar_name1 nvarchar(50)
  ,@TGuar_name2 nvarchar(50)
  ,@TChq_no bigint
 ,@TChq_date date
  ,@TGurAdd1 nvarchar(100)
  ,@TGurAdd2 nvarchar(100)
  ,@TGurPhone1 nvarchar(100)
  ,@TGurPhone2 nvarchar(100)
  ,@TPeriodInYear nvarchar(100)
  ,@TPeriod nvarchar(100)
AS
BEGIN --Act_no
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

-- Insert statements for procedure here
insert Loan_Master 
(
 Ac_no, Acc_no , Ac_name, V_no, Jlg_no, Post_no, Act_no, phone_no1, 
 Guar_name1 ,GurAdd1, GurPhone1, Guar_name2, 
 GurAdd2, GurPhone2, Loan_amt, Loan_date, Int_rate, 
 PeriodInYear, Pay_mode, Prin_inst, Chq_date, Chq_no, Period
)
values
(
 @TAc_no, @TAcc_no, @TAc_name, @TV_no, @TJlg_no, @TPost_no, @TAct_no, @TPhone_no1,  
 @TGuar_name1, @TGurAdd1, @TGurPhone1, @TGuar_name2,
 @TGurAdd2, @TGurPhone2, @TLoan_amt, @TLoan_date, @TInt_rate, 
 @TPeriodInYear, @TPay_mode, @TPrin_inst, @TChq_date, @TChq_no,  @TPeriod
)

答案 1 :(得分:1)

存储过程插入中的参数与传递的变量不匹配。

enter image description here