C#에서 SQL Server 연결에 문제가 있습니다.
내 프로그램은 간단하고 이름과 설명을 입력한 다음 데이터베이스에 Active Record와 함께 저장합니다. 그러나 데이터는 저장을 시작하지 않습니다.
SQL Server 데이터베이스( Select top 1000 rows
)를 참조할 때 아무 것도 표시되지 않습니다.
C#과 SQL 중 어느 부분이 작동하지 않습니까? 입력에 콘솔 명령을 사용하고 있습니다.
내 수업 Neighborhood
:
private string name;
private string description;
public string Name { get { return name; } set { name = value; } }
public string Description { get { return description; } set { description = value; } }
public Neighborhood(string name, string description)
{
this.Name = name;
this.Description = description;
}
public Neighborhood(){}
public bool Save()
{
bool retor = false;
string strCon = "Data Source=FACUNDO\\USARESTESQLSERVE; Initial Catalog = TestDB; Integrated Security = SSPI; ";
SqlConnection con = new SqlConnection(strCon);
string insert = "INSERT INTO Neighborhood (Name, Description) VALUES (@nom, @desc);";
SqlCommand command = new SqlCommand(insert, con);
command.Parameters.AddWithValue("@nam", this.Name);
command.Parameters.AddWithValue("@desc", this.Description);
try
{
con.Open();
retor = true;
}
catch
{
retor = false;
throw;
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return retor;
}
그리고 이것은 SQL 코드입니다.
CREATE DATABASE TestDB
USE TestDB
CREATE TABLE Neighborhood
(
Name VARCHAR(30) NOT NULL,
Description VARCHAR(50) NOT NULL,
CONSTRAINT PK_Neighborhood PRIMARY KEY (Name)
)
여기에 오타가 있습니다. 정답은 @nom입니다.
command.Parameters.AddWithValue("@nom", this.Name);
다음을 사용하여 쿼리를 실행하는 것을 잊었습니다 command.ExecuteNonQuery();
.
try
{
con.Open();
// execute the query command
command.ExecuteNonQuery();
retor = true;
}
catch
{
retor = false;
throw;
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return retor;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다