Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace qlks_new
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlconnection = new SqlConnection();
string connectionString = @"server = BHQCLIENT1" + "database = qlks" + "Integrated Security=True;";
sqlconnection.ConnectionString = connectionString;
try
{
sqlconnection.Open();
SqlCommand sqlcommand = new SqlCommand();
sqlcommand.CommandText = "Insert Into testtable" +
"(Ho_Ten,Lop)" +
"Values (@Ten,@Lop);";
SqlParameter sqlparameter = new SqlParameter();
sqlparameter.ParameterName = "@ten";
sqlparameter.SqlValue=textBox1;
sqlparameter.SqlDbType=SqlDbType.VarChar;
sqlparameter.Size=50;
sqlcommand.Parameters.Add(sqlparameter);
sqlparameter = new SqlParameter();
sqlparameter.ParameterName= "@lop";
sqlparameter.SqlValue=textBox2;
sqlparameter.SqlDbType=SqlDbType.VarChar;
sqlparameter.Size=20;
sqlcommand.Parameters.Add(sqlparameter);
sqlcommand.Connection = sqlconnection;
sqlcommand.ExecuteNonQuery();
sqlconnection.Close();
sqlconnection.Dispose();
MessageBox.Show("da thanh cong","Message",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("error" + ex.Message,"Message",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}
đây là đoạn code test của mình. khi click vào cái button nó báo lỗi là
an error has occurred while establishing a connection to the server . when connecting to SQL server 2005 , this failure may be caused by the fact that under the defaut setting SQL server does not allow remote connections (error 40 - can't open connection to SQL server )
mình đã chạy service của SQL 2000 rồi. trong máy của mình ko cài SQL 2005 mà khi cài bộ visual studio nó tự cài một bộ vào hay sao ấy, thấy có trong all program nhưng mở ra thấy chẳng có gì cả...
và mình cũng đã vào mục SQL server configura.. của 2005 đóng 2 cái service trong đó lại mà vẫn báo lỗi thế. chẳng hỉu gì cả
mình hok giỏi tiếng anh mấy . các bạn giúp m với +_+
bọn mình đang học môn SQL 2000 vì thế buộc phải xài 2000 để có thể demo ở trường được . mà m chẳng bít phải làm sao cả, các bạn giúp m với