2013-04-04 2 views
0

프로젝트 (Winforms 및 웹 서비스 프로젝트)에 각각 데이터베이스가 두 개 있으며 프로젝트 1에서 프로젝트 2로 데이터를 전송하는 Entity Framework를 사용하는 쿼리가 있습니다. 어떻게하면 첫 번째 데이터베이스의 이미지를 문자열을 쿼리를 통해 보낼 수 있습니까?SQL Server에서 이미지를 문자열로 변환하는 방법은 무엇입니까?

이 내 웹 서비스 코드 :

// Entity Framework 
Person sd = new Person(); 
// Method to get data from winforms app 
public void GetData(string name,string picture) 
{ 
    sd.name= name; 
    sd.picture= ImageToByteArray(picture); 
    context.AddToPerson(sd); 
    context.SaveChanges(); 
} 

//Method to save the image into database 
private Byte[] ImageToByteArray(string source) 
{ 
    FileInfo fInfo = new FileInfo(source); 
    long sizeByte = fInfo.Length; 
    FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read); 
    BinaryReader br = new BinaryReader(fs); 
    byte[] data = br.ReadBytes((int)sizeByte); 
    return data; 
} 

그리고 이것은 내 윈폼 코드입니다 : 내가 문자열로 이미지를 누군가 그렇다면하시기 바랍니다 변환하는 방법을 필요로하는 경우

WebService3SD.Service1SoapClient oService = new WebService3SD.Service1SoapClient(); 

private void SendData() 
{ 
    Driver dr = context.Drivers.FirstOrDefault(d => d.name == "name1"); 
    oService.GetData(dr.name,????);//here i have no idea what i have to do ?! 
} 

내가 그것에 대해 매우 높이 평가하게 될 것이라고 생각하는 어떤 있습니다.

+0

그것은 분명를'GetData' *이'picture' 문자열이 될 것으로 예상 서버 *의 파일 경로를 나타냅니다. (그것은'FileInfo'의 생성자에 전달됩니다.) 그래서 "이미지를 문자열로 변환"하는 것이 아니라 이미 서버에있는 파일에 경로를 전달해야합니다. 이것이 원하는 것이 아니면 로컬 경로를 기반으로 작동하지 않도록 서버를 구현해야합니다. –

+0

좋은 솔루션이 될 것 ** Kirk **하지만 어떻게 코딩 할 수 있습니까? – Mohammadov

답변

0

아마도 전송을 위해 이미지를 Base64로 인코딩하려고합니다. 지금 코드가 서버 파일 시스템을 읽으려고합니다.

private void btnEncode_Click(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrEmpty(txtInFile.Text)) 
    { 
    FileStream fs = new FileStream(txtInFile.Text, 
            FileMode.Open, 
            FileAccess.Read); 
    byte[] filebytes = new byte[fs.Length]; 
    fs.Read(filebytes, 0, Convert.ToInt32(fs.Length)); 
    string encodedData = 
     Convert.ToBase64String(filebytes,     
           Base64FormattingOptions.InsertLineBreaks); 
    txtEncoded.Text = encodedData; 
    } 
} 

수신 측 : 요청하게 앱에

다음과 같이 코드를 참조

private void btnDecode_Click(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrEmpty(txtOutFile.Text)) 
    { 
    byte[] filebytes = Convert.FromBase64String(txtEncoded.Text); 
    FileStream fs = new FileStream(txtOutFile.Text, 
            FileMode.CreateNew, 
            FileAccess.Write, 
            FileShare.None); 
    fs.Write(filebytes, 0, filebytes.Length); 
    fs.Close(); 
    } 
} 
+0

고맙습니다.하지만 첫 번째 방법으로 이미지를 어디에 두어야합니까? – Mohammadov

관련 문제