2012-10-07 45 views
1

我想把一些數據放到MSSQL數據庫中,我用Visual Basic創建數據庫。當我執行的代碼,我得到以下錯誤:添加數據到mssql字符串到字節錯誤c#

Error 21 Cannot implicitly convert type 'string' to 'byte[]' c:\users\hussein\documents\visual studio 2012\Projects\WebApplication2\WebApplication2\defualt.aspx.cs 32 33 WebApplication2 
Error 22 Cannot implicitly convert type 'string' to 'byte[]' c:\users\hussein\documents\visual studio 2012\Projects\WebApplication2\WebApplication2\defualt.aspx.cs 33 32 WebApplication2 
Error 23 Cannot implicitly convert type 'string' to 'byte[]' c:\users\hussein\documents\visual studio 2012\Projects\WebApplication2\WebApplication2\defualt.aspx.cs 34 34 WebApplication2 
Error 24 Cannot implicitly convert type 'string' to 'byte[]' c:\users\hussein\documents\visual studio 2012\Projects\WebApplication2\WebApplication2\defualt.aspx.cs 35 34 WebApplication2 

這裏是我的代碼,我試圖將字符串轉換爲byte[],但我有我的數據庫二進制文本。

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Data; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Xml.Linq; 
using System.Linq; 
using System.Data.Linq; 

namespace WebApplication2 
{ 
    public partial class defualt : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Unnamed5_Click(object sender, EventArgs e) 
     { 
      if ((TextBox1.Text == "" || TextBox2.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")) 
      { 
       Label1.Text = "<h3>- Du måste fylla i alla fält, brorsan</h3>"; 
      } 
      else 
      { 
       DatabaseEntities db = new DatabaseEntities(); 
       var nyMedlem = new medlemar(); 
       nyMedlem.namn = TextBox1.Text; 
       nyMedlem.anv = TextBox2.Text; 
       nyMedlem.losen = TextBox3.Text; 
       nyMedlem.epost = TextBox4.Text; 
       db.medlemar.Add(nyMedlem); 
       db.SaveChanges(); 
       Label1.Text = "<h3>- Nu är du medlem</h3>"; 
      } 
     } 
    } 
} 

回答

1

您可能將4個文本字段保存爲格式爲byte []的數據庫字段,因此您將獲得4次轉換。

試試這個:它需要改變medlemar類。

string x = TextBox1.Text; 
byte[] y = System.Text.Encoding.UTF8.GetBytes(x); 

nyMedlem.(something of data type byte[]) = y;