Tak for eksemplet,
jeg har nu det her kode
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Thread thrDownload;
private Stream strResponse;
private Stream strLocal;
private HttpWebRequest webRequest;
private HttpWebResponse webResponse;
private static int PercentProgress;
private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
bool goPause = false;
SqlConnection myConnection = new SqlConnection("password=ijujkuo8;server=C:/Documents and Settings/Administrator.COMPAQMINIDesktop/DownloadManager.sdf;" + "Trusted_Connection=yes;" + "database=database; " + "connection timeout=30");
public Form1()
{
InitializeComponent();
}
private void BtnDownload_Click(object sender, EventArgs e)
{
if (thrDownload != null && thrDownload.ThreadState == ThreadState.Running){
MessageBox.Show("A download is already running. Please either stop the current download or await for it's completion before starting a new one.", "Download in progress", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else {
StatusLbl.Text = "Download starting";
thrDownload = new Thread(new ParameterizedThreadStart(Download));
thrDownload.Start(0);
BtnPause.Enabled = true;
BtnPause.Text = "Pause";
}
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes) {
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
prgDownload.Value = PercentProgress;
StatusLbl.Text = "Download " + BytesRead + " out of " + TotalBytes + "( " + PercentProgress + "%)";
}
private void Download(Object startPoint) {
try
{
int startPointInt = Convert.ToInt32(startPoint);
webRequest = (HttpWebRequest)WebRequest.Create(TxtUrl.Text);
webRequest.AddRange(startPointInt);
webRequest.Credentials = CredentialCache.DefaultCredentials;
webResponse = (HttpWebResponse)webRequest.GetResponse();
Int64 filesize = webResponse.ContentLength;
strResponse = webResponse.GetResponseStream();
if (startPointInt == 0)
{
strLocal = new FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None);
}
else
{
strLocal = new FileStream(TxtUrl.Text, FileMode.Append, FileAccess.Write, FileShare.None);
}
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, filesize + startPointInt });
if (goPause == true)
{
break;
}
}
}
finally {
strResponse.Close();
strLocal.Close();
}
}
private void BtnStop_Click(object sender, EventArgs e)
{
thrDownload.Abort();
webResponse.Close();
strResponse.Close();
strLocal.Close();
prgDownload.Value = 0;
StatusLbl.Text = "Download stopped";
BtnPause.Enabled = false;
}
private void BtnPause_Click(object sender, EventArgs e)
{
if (thrDownload != null)
{
if (BtnPause.Text == "Pause")
{
goPause = true;
BtnPause.Text = "Resume";
webResponse.Close();
strResponse.Close();
strLocal.Close();
thrDownload.Abort();
}
else
{
goPause = false;
BtnPause.Text = "Pause";
long startpoint = 0;
if (File.Exists(txtPath.Text))
{
startpoint = new FileInfo(txtPath.Text).Length;
}
else
{
MessageBox.Show("The file you choosed to resume doesn't exist.", "Could not resume download", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
StatusLbl.Text = "Download resuming";
thrDownload = new Thread(new ParameterizedThreadStart(Download));
thrDownload.Start(startpoint);
BtnPause.Enabled = true;
}
}
else {
MessageBox.Show("A download does not appear to be in progress.", "Could not pause", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void TxtUrl_TextChanged(object sender, EventArgs e)
{
string[] filename = TxtUrl.Text.Split(("/").ToCharArray());
int filenameLength = 0;
foreach (string name in filename)
{
filenameLength += 1;
}
filenameLength += -1;
txtPath.Text = "C:/download/"+filename[filenameLength];
}
private void button1_Click(object sender, EventArgs e)
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM Downloads", myConnection);
SqlDataReader myReader = null;
myReader = myCommand.ExecuteReader();
while(myReader.Read())
{
textBox1.Text = myReader["Column1"].ToString();
}
}
}
}
Men får følgende fejl
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
i denne linje
myConnection.Open();
og kan ikke se hvad det er jeg har gjort forkert.
er der nogle som kan hjælpe mig lidt her?