我在C中编写了一个程序,它使用Embedded Mono调用我的C#库。 我已经有了它的工作,但我遇到了在我的网络上进行SQL调用的问题。
正常运行,当我尝试连接数据库时出现以下异常:
System.ArgumentException:值不在预期范围内 范围。\ r \ n在System.Net.Sockets.Socket.SetSocketOption (System.Net.Sockets.SocketOptionLevel optionLevel, System.Net.Sockets.SocketOptionName optionName,System.Int32 optionValue)[0x00061]在< 5071a6e4a4564e19a2eda0f53e42f9bd>中:0 \ r \ n 在Mono.Data.Tds.Protocol.TdsComm..ctor(System.String dataSource, System.Int32端口,System.Int32 packetSize,System.Int32超时, Mono.Data.Tds.Protocol.TdsVersion tdsVersion)
奇怪的是,当我在 gdb 中运行它时(尝试处理正在传递的值等等),一切正常,我的数据库调用成功。
有没有人有任何想法?
我在Windows 7中运行,我的C代码是使用minGW中的gcc编译的:
gcc test.c -mms-bitfields -ID:/Mono/include/mono-2.0 -LD:/ Mono / lib -lmono-2.0 -mms-bitfields -lws2_32 -lpsapi -lole32 -lwinmm -loleaut32 -ladvapi32 - lversion
更新: 使用以下连接字符串,它现在可以在10个中使用2次: Server = 192.168.1.54; Database = MyDb; User Id = my_user; Password = mypwd123; Integrated Security = False
#include <mono/jit/jit.h>
#include <mono/metadata/object.h>
#include <mono/metadata/environment.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
static MonoMethod *getMethod(char* methodName)
{
static MonoImage *img;
if(img == NULL)
{
MonoDomain *domain = mono_jit_init("my.dll");
if(domain == NULL)
{
return NULL;
}
MonoAssembly *assembly = mono_domain_assembly_open(domain, "my.dll");
if(assembly == NULL)
{
return NULL;
}
img = mono_assembly_get_image(assembly);
if(img == NULL)
{
return NULL;
}
}
MonoMethodDesc *desc = mono_method_desc_new(methodName, true);
if(desc == NULL)
return NULL;
return mono_method_desc_search_in_image(desc, img);
}
int main(void)
{
MonoMethod *method = getMethod("MyNs.MyClass:MyFunc(string,string)");
if(method == NULL)
{
puts("Method was null");
return 1;
}
MonoObject* ex = NULL;
MonoObject *res = mono_runtime_invoke(method, NULL, NULL, &ex);
if(ex != NULL)
{
char *s = mono_string_to_utf8(mono_object_to_string(ex, NULL));
puts(s);
return 2;
}
if(res == NULL)
{
puts("No result");
return 3;
}
char *rv = mono_string_to_utf8(mono_object_to_string(res, NULL));
puts(rv);
return 0;
}
C#代码:
public static string MyFunc(string connectionString, string query)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
return reader.GetString(0);
}
}
return "No data found";
}