2013-08-19 148 views
0

我想將數據插入數據庫。我想將數據插入數據庫

string pa = "4898"; 
string equery="Insert into Users(pas)values('"+pa+"')"; 

雖然我將數據插入到數據庫中,但它的字符串或二進制數據將被截斷,語句已被終止。所以我在表格中將nvarchar(50)更改爲nvarchar(max)

而這些語句已被執行並保存在數據庫中的不可讀格式如傉䝎਍ਚ這個。如何解決這個問題,並將數據保存爲「4898」。

private void save_Click(object sender, EventArgs e) 
{ 
    string password = "4898"; 
    string equery="Insert into Users(name,gender,dateofbirth,age,fathername, 
             address,citiy,state,country,zipcode, 
             mobile,phone,email,jobtitle, 
             dateofjoin,pic,passwords) 
          values('"+nametxt.Text.ToString().Trim()+"', 
            @gender, 
            '"+dateofbirth.Text.ToString().Trim()+"', 
            '"+age.Value.ToString().Trim()+"', 
            '"+fathertxt.Text.ToString().Trim()+"', 
            '"+addresstxt.Text.ToString().Trim()+"', 
            '"+citiytxt.Text.ToString().Trim()+"', 
            '"+statetxt.Text.ToString().Trim()+"', 
            '"+country.Text.ToString().Trim()+"', 
            '"+ziptxt.Text.ToString().Trim()+"', 
            '"+mobiletxt.Text.ToString().Trim()+"', 
            '"+phonetxt.Text.ToString().Trim()+"', 
            '"+emailtxt.Text.ToString().Trim()+"', 
            '"+jobtxt.Text.ToString().Trim()+ 
            "','"+dateofjoin.Text.ToString().Trim()+"', 
            '"+password+"',@pic)"; 
    SqlCommand cmd = new SqlCommand(equery, con); 

    if(male.Checked) 
     cmd.Parameters.AddWithValue("@gender","Male"); 
    else 
     cmd.Parameters.AddWithValue("@gender","Female"); 

    MemoryStream stream = new MemoryStream(); 
    pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Png); 
    byte[] pic = stream.ToArray(); 
    cmd.Parameters.AddWithValue("@pic", pic); 

    try 
    { 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     MessageBox.Show("Saved Successfully"); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    finally 
    { 
     con.Close(); 
    } 

    MessageBox.Show("Saved Successfully"); 
    idtxt.Text = ""; 
    nametxt.Text = ""; 
    age.Value = Convert.ToDecimal(null); 
    male.Checked = false; 
    female.Checked = false; 
    dateofbirth.Text = ""; 
    fathertxt.Text = ""; 
    addresstxt.Text = ""; 
    citiytxt.Text = ""; 
    statetxt.Text = ""; 
    country.Text = ""; 
    ziptxt.Text = ""; 
    mobiletxt.Text = ""; 
    phonetxt.Text = ""; 
    emailtxt.Text = ""; 
    dateofjoin.Text = ""; 
    jobtxt.Text = ""; 
    pictureBox1.Image = null; 
} 
+1

''是一個解釋爲UTF-16的PNG圖像的開始。 – icktoofay

+0

請將制動點設置爲equery並複製該查詢並嘗試在sql server上運行。檢查數據是否正在插入.... –

+0

在調試過程中顯示錶結構和equery的值 – kbvishnu

回答

4

看你的插入語句:

"Insert into Users(... pic,passwords) values (... '"+password+"',@pic)"; 

看來你互換picpasswords


此外,正如其他人已經在評論中指出的,你應該只使用參數化查詢(以防止SQL注入),處理由用戶或其他任何不受信任來源插入的文本時尤其如此。

0

代碼中存在邏輯錯誤。互換

'"+password+"',@pic)"; 

在您的字符串equery。 順便提一個建議!最好使用'linq to Sql'而不是查詢傳遞方法!因爲'Linq to Sql'會爲你做所有事情。你只需要給出值:)

+0

LINQ to SQL不適合所有情況,例如它與SQL CE數據庫不兼容。在某些情況下,當一個簡單的查詢就足夠時,它也會給程序增加不必要的開銷。 – Amicable

+0

@Amicable哦,我不知道! :p感謝信息想:) –